type Integer<T extends number> = number extends T ? never : `${T}` extends `${any}.${any}` ? never : T
Solution by wendao-liu #35107
type Integer${T}
extends ${any}.${any}
? never : T
Solution by rookiewxy #34157
type Integer<T extends number> = number extends T ? never : `${T}` extends `${string}.${string}` ? never : T
Solution by ouzexi #34119
type Integer<T extends number> = number extends T
? never
: `${T}` extends `${infer Num1 extends number}.${infer Num2 extends number}`
? Num2 extends 0
? Num1
: never
: T
Solution by PiligrimStas #33807
type Integer<T extends number> = number extends T
? never
: `${T}` extends `${any}.${any}`
? never
: T;
Solution by sunupupup #33468
// your answers
type Integer<T extends number> = number extends T
? never
: `${T}` extends `${infer Int extends number}.${infer Float}`
? never
: T;
Solution by pea-sys #33369
type Integer<T> = T extends number
? `${T}` extends `${number}.${number}`
? never
: T
: never```
Solution by keyurparalkar #32722
// your answers
type Integer<T> = number extends T
? never
: T extends number
? `${T}` extends `${number}.${number}`
? never
: T
: never;
Solution by kerolossamir165 #29444
type Integer<T extends number> = number extends T
? never
: `${T}` extends `${infer Int extends number}.${infer Float}`
? never
: T;
Solution by DoubleWoodLin #28863
type RemoveZero<T extends string> = T extends `${infer Num}0`
? RemoveZero<Num>
: T;
type Integer<
T extends number,
U extends string = RemoveZero<`${T}`>
> = U extends `${infer Int extends number}.${infer Float}`
? Float extends ""
? Int
: never
: U extends `${infer Num extends number}`
? number extends Num
? never
: Num
: never;
Solution by DoubleWoodLin #28861
type Integer<T extends number> = `${T}` extends `${string}.${string}`
? never
: number extends T
? never
: T;
Solution by DoGukKim #28449
判断是不是整数有两个关键,浮点数整数,正整数
number extends T ? never : ...:如果 T 是 number 类型或 number 类型的子类型,按照上面的讨论,通常情况下会进入 : ... 这个分支。如果 T 是 string 类型,会匹配到 never。
${T}:将 T 转换为模板文字类型。如果 T 是 number 类型,这将把数字转换为字符串。例如,如果 T 是 42,${T} 将会是字符串 "42"。
${T} extends ${string}.${string} ? never : T
:接下来检查 ${T}(数字变为字符串的结果,如"42")是否符合 ${string}.${string}
的格式。这是一个字符串,其中包含一个或多个字符、一个点号,后跟一个或多个字符。简单地说,就是用来检查 T 是否包含一个小数点。
type Integer<T extends string | number> = number extends T
? never
: `${T}` extends `${string}.${string}`
? never
: T
Solution by linjunc #28261
type Integer<T extends any> = T extends Number ? T : never;
Solution by 20yuteo #27782
type Integer<T extends number> = number extends T ?
never :
`${T}` extends `${string}.${string}` ?
never :
T
Solution by jjswifty #27386
type Integer<T extends number> = number extends T ? never : `${T}` extends `${number}.${number}` ? never : T
Solution by smileboyi #27254
// your answers
type Integer<T extends number> = number extends T
? never
: `${T}` extends `${number}.${'' | number}`
? never
: T
Solution by studymachiney #24691
type Integer<T extends number> = number extends T ? never : `${T}` extends `${any}.${any}` ? never : T;
// fantastic way
// type Integer<T extends number> = `${T}` extends `${bigint}` ? T : never
Solution by E-uler #24552
type Integer<T extends number> = number extends T
? never
: `${T}` extends `${number}.${'' | number}`
? never
: T
Solution by NeylonR #24372
type Integer<T extends number> = number extends T
? never
// 1.0 => 1 ref: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-8.html#improved-inference-for-infer-types-in-template-string-types
: `${T}` extends `${infer R extends number}`
// Filter decimal
? `${T}` extends `${infer I extends number}.${infer D extends number}`
? never
: R
: never;
Solution by nia3y #23372
// your answers
type Integer<T> = `${T & number}` extends `${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}` ? 1 : never
Solution by snakeUni #23331
// 你的答案
type Integer<T> = `${T & number}` extends `${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}` ? 1 : never
Solution by jxhhdx #22755
type Integer<T extends number> = `${T}` extends `${bigint}` ? T : never
Solution by teamchong #22533
type Integer<T extends number> =
number extends T
? never
: `${T}` extends `${infer _A}.${infer _B}`
? never
: T
Solution by drylint #22126
type Integer<T extends number> = number extends T
? never
: `${T}` extends `${infer I}.${infer R}`
? never
: T
Solution by jgjgill #21579
type Integer<T> =
T extends number
? `${T}` extends `${infer F}.${infer L}`
? L extends 0 ? F : never
: number extends T ? never : T
: never;
Solution by ivbrajkovic #21528
type Integer<T extends number> = `${T}` extends `${infer F}.${infer L}`
? L extends 0
? F
: never
: number extends T ? never : T
Solution by milletlovemouse #21447
type Integer<T extends number> = `${T}` extends `${infer I}.${infer Last}` ? never: number extends T? never: T;
Solution by BruceYuj #21293
type Integer<T> = number extends T ? never : T extends number
? `${T}` extends `${number}.${string}`
? never
: T
: never;
Solution by venusliang #20888
type Integer<T extends number> =
`${T}` extends `${infer A}.${infer B extends 0}`
? A
: `${T}` extends `${infer G extends number}${infer P}`
? T extends G
? number extends G
? never
: G
: never
: never;
Solution by so11y #20830
// your answers
type Integer<T extends number> = `${T}` extends `${infer F}.${infer G}` ? never : number extends T ? never : T
Solution by YqxLzx #20250