Absolute
介绍
实现一个接收string,number或bigInt类型参数的Absolute
类型,返回一个正数字符串。
ts
type Test = -100;type Result = Absolute<Test>; // expected to be "100"
View on GitHubts
type Test = -100;type Result = Absolute<Test>; // expected to be "100"
起点
ts
/* _____________ Your Code Here _____________ */typeAbsolute <T extends number | string | bigint> = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <0>, '0'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <-0>, '0'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <10>, '10'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <-5>, '5'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <'0'>, '0'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <'-0'>, '0'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <'10'>, '10'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <'-5'>, '5'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <-1_000_000n>, '1000000'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <9_999n>, '9999'>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeAbsolute <T extends number | string | bigint> = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <0>, '0'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <-0>, '0'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <10>, '10'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <-5>, '5'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <'0'>, '0'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <'-0'>, '0'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <'10'>, '10'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <'-5'>, '5'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <-1_000_000n>, '1000000'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Absolute <9_999n>, '9999'>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
// most populartypeAbsolute <T extends number | string | bigint> = `${T }` extends `-${inferU }` ?U : `${T }`
ts
// most populartypeAbsolute <T extends number | string | bigint> = `${T }` extends `-${inferU }` ?U : `${T }`
ts
// 没啥用typeAbsolute <T extends number | string | bigint> =T extends number? `${T }` extends `-${inferTn }` ? `${Tn }` : `${T }`:T extends string? `${T }` extends `-${inferTs }` ? `${Ts }` : `${T }`:T extends bigint? `${T }` extends `-${inferTb }`? `${Tb }`: `${T }`: never
ts
// 没啥用typeAbsolute <T extends number | string | bigint> =T extends number? `${T }` extends `-${inferTn }` ? `${Tn }` : `${T }`:T extends string? `${T }` extends `-${inferTs }` ? `${Ts }` : `${T }`:T extends bigint? `${T }` extends `-${inferTb }`? `${Tb }`: `${T }`: never