Implement the type version of Math.trunc
, which takes string or number and returns the integer part of a number by removing any fractional digits.
For example:
type A = Trunc<12.34> // 12
/* _____________ Your Code Here _____________ */
type Trunc<N extends number | string> = `${N}` extends `${infer NUM}.${infer _}`
? `${NUM}`
: `${N}`;
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Trunc<0.1>, '0'>>,
Expect<Equal<Trunc<1.234>, '1'>>,
Expect<Equal<Trunc<12.345>, '12'>>,
Expect<Equal<Trunc<-5.1>, '-5'>>,
Expect<Equal<Trunc<'1.234'>, '1'>>,
Expect<Equal<Trunc<'-10.234'>, '-10'>>,
Expect<Equal<Trunc<10>, '10'>>,
]
标签:Typescript,_____________,number,Medium,Expect,71,NUM,type,Trunc From: https://www.cnblogs.com/Answer1215/p/16832136.html