529 - Absolute
原创2025/4/29小于 1 分钟
529 - Absolute
by Andrey Krasovsky (@bre30kra69cs) #中等 #math #template-literal
题目
实现一个接收string,number或bigInt类型参数的Absolute
类型,返回一个正数字符串。
例如
type Test = -100;
type Result = Absolute<Test>; // expected to be "100"
在 Github 上查看:https://tsch.js.org/529/zh-CN
代码
/* _____________ 你的代码 _____________ */
type Absolute<T extends number | string | bigint> = `${T}` extends `-${infer R}` ? R : `${T}`
测试用例
/* _____________ 测试用例 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Absolute<0>, '0'>>,
Expect<Equal<Absolute<-0>, '0'>>,
Expect<Equal<Absolute<10>, '10'>>,
Expect<Equal<Absolute<-5>, '5'>>,
Expect<Equal<Absolute<'0'>, '0'>>,
Expect<Equal<Absolute<'-0'>, '0'>>,
Expect<Equal<Absolute<'10'>, '10'>>,
Expect<Equal<Absolute<'-5'>, '5'>>,
Expect<Equal<Absolute<-1_000_000n>, '1000000'>>,
Expect<Equal<Absolute<9_999n>, '9999'>>,
]
相关链接
分享你的解答:https://tsch.js.org/529/answer/zh-CN
查看解答:https://tsch.js.org/529/solutions
更多题目:https://tsch.js.org/zh-CN