跳到主要内容

Trunc

介绍

实现Math.trunc, 接收字符串或数值,移除小数位,返回正数值

ts
type A = Trunc<12.34> // 12
ts
type A = Trunc<12.34> // 12
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
type Trunc<T> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<Trunc<0.1>, '0'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<0.2>, '0'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<1.234>, '1'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<12.345>, '12'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<-5.1>, '-5'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<'.3'>, '0'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<'1.234'>, '1'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<'-.3'>, '-0'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<'-10.234'>, '-10'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<10>, '10'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
ts
/* _____________ Your Code Here _____________ */
 
type Trunc<T> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<Trunc<0.1>, '0'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<0.2>, '0'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<1.234>, '1'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<12.345>, '12'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<-5.1>, '-5'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<'.3'>, '0'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<'1.234'>, '1'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<'-.3'>, '-0'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<'-10.234'>, '-10'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trunc<10>, '10'>>,
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
type Trunc<T extends number|string> = `${T}` extends `${infer A}.${any}`
? A extends ''|'-'
? A extends '' ? '0' : '-0'
: A
: `${T}`
 
ts
type Trunc<T extends number|string> = `${T}` extends `${infer A}.${any}`
? A extends ''|'-'
? A extends '' ? '0' : '-0'
: A
: `${T}`
 
view more solutions