IsNegativeNumber
题目
Sometimes when working with numeric literals, we need to rule out (or enforce) that the provided number is a positive integer.
To do that, we first need a way to tell if the number is negative.
Write a type-level function IsNegativeNumber
that accepts a number N
and returns:
true
ifN
is negativefalse
ifN
is positivefalse
ifN
is0
,never
ifN
isnumber
never
ifN
is a union
解答
ts
type IsUnion<T, U = T> = U extends T ? ([T] extends [U] ? false : true) : never;
type IsNegativeNumber<T extends number> = IsUnion<T> extends true
? never //union
: number extends T
? never //number
: `${T}` extends `-${number}`
? true
: false; //negative number