跳到主要内容

Absolute

介绍

实现一个接收string,number或bigInt类型参数的Absolute类型,返回一个正数字符串。

ts
type Test = -100;
type Result = Absolute<Test>; // expected to be "100"
ts
type Test = -100;
type Result = Absolute<Test>; // expected to be "100"
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
type Absolute<T extends number | string | bigint> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
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'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
ts
/* _____________ Your Code Here _____________ */
 
type Absolute<T extends number | string | bigint> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
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'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
take the challenge

解决方案

Spoiler warning // Click to reveal answer
ts
// most popular
 
type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer U}` ? U : `${T}`
ts
// most popular
 
type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer U}` ? U : `${T}`
ts
// 没啥用
 
type Absolute<T extends number | string | bigint> = T extends number
? `${T}` extends `-${infer Tn}` ? `${Tn}` : `${T}`
: T extends string
? `${T}` extends `-${infer Ts}` ? `${Ts}` : `${T}`
: T extends bigint
? `${T}` extends `-${infer Tb}`
? `${Tb}`
: `${T}`
: never
ts
// 没啥用
 
type Absolute<T extends number | string | bigint> = T extends number
? `${T}` extends `-${infer Tn}` ? `${Tn}` : `${T}`
: T extends string
? `${T}` extends `-${infer Ts}` ? `${Ts}` : `${T}`
: T extends bigint
? `${T}` extends `-${infer Tb}`
? `${Tb}`
: `${T}`
: never
view more solutions