Trunc
介绍
实现Math.trunc
, 接收字符串或数值,移除小数位,返回正数值
ts
type A = Trunc<12.34> // 12
View on GitHubts
type A = Trunc<12.34> // 12
起点
ts
/* _____________ Your Code Here _____________ */typeTrunc <T > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.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'>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeTrunc <T > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.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'>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
typeTrunc <T extends number|string> = `${T }` extends `${inferA }.${any}`?A extends ''|'-'?A extends '' ? '0' : '-0':A : `${T }`
ts
typeTrunc <T extends number|string> = `${T }` extends `${inferA }.${any}`?A extends ''|'-'?A extends '' ? '0' : '-0':A : `${T }`