type Odd = 1 | 3 | 5 | 7 | 9;
type IsOdd<T extends number> =
`${T}` extends `${string}.${string}`
? false
: `${T}` extends `${infer F extends number}e${infer R extends number}`
? R extends 0
? IsOdd<F>
: false
: `${T}` extends `${number | ''}${Odd}`
? true
: false;
Consider cases where the exponent of e is 0.
example, 3e0
= 3×10^0= 3×1 = 3 = odd.
but 2e0
= 2×10^0 = 2×1 = 2 = even.
Solution by ysknsid25 #35226
type IsOdd<T extends number> =
`${T}` extends `${infer _}${1 | 3 | 5 | 7 | 9}`
? true
: false
Solution by drylint #34178
type Last<T extends string> = T extends `${infer R}${infer last}` ? last extends '' ? R : Last<last> : never
type IsOdd<T extends number> = `${T}` extends `${string}${'.' | 'e'}${string}` ? false : Last<`${T}`> extends `${1 | 3 | 5 | 7 | 9}` ? true : false
Solution by ouzexi #34173
type IsOdd<T extends number> = number extends T
? false
: `${T}` extends `${number}.${number}` | `${number}${0|2|4|6|8}` | `${number}e${number}`
? false
: true
Solution by ruslauz #33812
type IsOdd<T extends number> = `${T}` extends `${bigint | ''}${1 | 3 | 5 | 7 | 9}` ? true : false;
Solution by sunupupup #33491
type IsOdd<T extends number>
= `${T}` extends `${any}e${any}` | `${any}.${any}` ? false
: `${T}` extends `${any}${1 | 3 | 5 | 7 | 9}` ? true : false
Solution by teamchong #32994
type OddNumbers = '1' | '3' | '5' | '7' | '9'
type GetLastDigit<T extends number | string, Acc extends string = ""> = `${T}` extends `${infer F}${infer R}`
? GetLastDigit<R, F>
: Acc
type IsExpo<T extends number> = `${T}` extends `${number}e${number}`
? true
: false
type IsDecimal<T extends number> = `${T}` extends `${number}.${number}`
? true
: false
type IsOdd<T extends number> = IsExpo<T> extends true
? false
: IsDecimal<T> extends true
? false
: GetLastDigit<T> extends OddNumbers
? true
: false
Solution by keyurparalkar #32884
type OddNumbers = '1' | '3' | '5' | '7' | '9'
type GetLastDigit<T extends number | string, Acc extends string = ""> = `${T}` extends `${infer F}${infer R}`
? GetLastDigit<R, F>
: Acc
type IsExpo<T extends number> = `${T}` extends `${number}e${number}`
? true
: false
type IsDecimal<T extends number> = `${T}` extends `${number}.${number}`
? true
: false
type IsOdd<T extends number> = IsExpo<T> extends true
? false
: IsDecimal<T> extends true
? false
: GetLastDigit<T> extends OddNumbers
? true
: false
Solution by keyurparalkar #32883
type Odd = 1 | 3 | 5 | 7 | 9;
type IsOdd<T extends number> = `${T}` extends `${string}${Odd}` ? true : false;
Solution by Vampirelee #32595
type IsOdd<T extends number> = `${T}` extends `${string}${1 | 3 | 5 | 7 | 9}`
? true
: false;
Solution by vangie #32194
type Odd = '1' | '3' | '5' | '7' | '9';
type IsOdd<T extends number> = `${T}` extends `${string}${Odd}` ? true : false;
Solution by bigcreate #31216
type IsOdd<T extends number> = `${T}` extends `${infer _}${1 | 3 | 5 | 7 | 9}` ? true : false
Solution by milletlovemouse #31083
type GetLastChar<
T extends number,
U extends string = `${T}`
> = U extends `${infer F}${infer R}`
? R extends ""
? F extends `${infer Res extends number}`
? Res
: F
: GetLastChar<T, R>
: T;
type IsOdd<
T extends number,
D extends number = GetLastChar<T>,
U extends any[] = [],
F extends boolean = false
> = U["length"] extends D
? F
: IsOdd<T, D, [...U, 0], F extends false ? true : false>;
Solution by moonpoet #31059
type MakeArray<T extends number | string, U extends unknown[] = []> = `${U['length']}` extends `${T}`
? U
: MakeArray<T, [...U, unknown]>;
type oddNumber = 1 | 3 | 5 | 7 | 9;
type LastString<S extends string> = S extends `${infer F}${infer L}` ? (L extends '' ? F : LastString<L>) : S;
type IsOdd<T extends number, U extends unknown[] = MakeArray<LastString<`${T}`>>> = U['length'] extends oddNumber
? true
: false;
Solution by leejaehyup #30945
type IsOdd<T extends number> = `${T}` extends `${any}${'1' | '3' | '4' | '7' | '9'}` ? true : false
Solution by DevilTea #30908
type Num2Arr<T extends number | string> = `${T}` extends `${infer A}${infer B}` ? [A, ...Num2Arr<B>] : []
type IsOdd<T extends number> = number extends T ? false : Num2Arr<T> extends [...infer A, infer B] ? B extends '1' | '3' | '5' | '7' | '9' ? true : false : false
Solution by dreamluo-plus #30716
type LastWord<T extends string> = T extends `${infer F}${infer R}`
? R extends ''
? F
: LastWord<R>
: never;
type IsOdd<T extends number> = LastWord<`${T}`> extends '1' | '3' | '5' | '7' | '9' ? true : false
Solution by XkSuperCool #30341
// your answers
type IsOdd<T extends number> = `${T}` extends `${number | ''}${1 | 3 | 5 | 7 | 9}` ? true : false;
Solution by omittee #30334
// your answers
type OddNume = "1" | "3" |"5" | "7" |"9"
type GetTheLastNumber<T extends string> = T extends `${infer F}${infer L}` ? L extends "" ? F : GetTheLastNumber<L> : never
type IsOdd<T extends number> = GetTheLastNumber<`${T}`> extends OddNume ? true : false
Solution by kerolossamir165 #30328
type Odd = 1 | 3 | 5 | 7 | 9;
type Last<T extends string> = T extends `${any}${infer S}${infer R}` ? Last<`${S}${R}`> : T;
type IsOdd<T extends number> = Last<`${T}`> extends `${Odd}` ? true : false;
// other way
// type Last<T extends string> = T extends `${any}${infer S}${infer R}` ? Last<`${S}${R}`> : T;
// type IsOdd<T extends number, _SWITCH extends boolean = false, _Counter extends 1[] = []> =
// `${_Counter[`length`]}` extends Last<`${T}`> ? _SWITCH /*return*/ :
// IsOdd<T, _SWITCH extends true ? false : true, [..._Counter, 1]>;
// simple way
// type IsOdd<T extends number> = `${T}` extends `${number}${1 | 3 | 5 | 7 | 9}` ? true : false;
simple way : https://github.com/type-challenges/type-challenges/issues/30334#issue-1929313282
Solution by E-uler #30324
type ToString<T extends number> = `${T}`
type LastDigit<T extends string> =
T extends `${infer F}${infer L}`?
L extends ""?
F:LastDigit<L>
:never
type Match<T extends string> = T extends '1' | '3' | '5' | '7' | '9'?true:false
type IsOdd<T extends number> = Match<LastDigit<ToString<T>>>
Solution by jiangshanmeta #30303