[Typescript] 71. Medium - Trunc

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'>>,
]

 

posted @   Zhentiw  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-10-27 [Kotlin] Destructuring and ComponentN functions
2020-10-27 [Kotlin] implicit getterr && setter (field keyword)
2020-10-27 [Kotlin] Multi ways to write constuctor in Kotlin
2019-10-27 [Kubernetes] Defining a Pod with YAML
2019-10-27 [Kubernetes] Kubectl and Pod
2016-10-27 [CSS] Use Generated Content to Augment Information
2016-10-27 [Angular2 Form] Reactive Form, show error message for one field
点击右上角即可分享
微信分享提示