首页 > 其他分享 >Typescript类型体操 - Trunc

Typescript类型体操 - Trunc

时间:2022-10-24 23:23:20浏览次数:60  
标签:Typescript number 体操 type any Math Trunc

题目

中文

实现类型版本的 Math.trunc, 其接受一个字符串或数字作为泛型参数, 并返回移除了全部小数位部分后的整数

示例:

type A = Trunc<12.34>; // 12

English

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

答案

type Trunc<T extends string | number> = T extends number
    ? Trunc<`${T}`>
    : T extends `${infer L}.${any}`
    ? L
    : T;

在线演示

标签:Typescript,number,体操,type,any,Math,Trunc
From: https://www.cnblogs.com/laggage/p/type-challenge-trunc.html

相关文章

  • Typescript类型体操 - TrimRight
    题目中文实现TrimRight<T>,它接收确定的字符串类型并返回一个新的字符串,其中新返回的字符串删除了原字符串结尾的空白字符串。例如typeTrimed=TrimRight<'Hello......
  • TypeScript 高级类型
    一、高级类型class类型兼容性交叉类型泛型和keyof索引签名类型和索引查询类型映射类型二、class构造函数给变量赋值extends继承implement实现接口//感叹......
  • React-hooks+TypeScript最佳实战
    ReactHooks什么是HooksReact一直都提倡使用函数组件,但是有时候需要使用state或者其他一些功能时,只能使用类组件,因为函数组件没有实例,没有生命周期函数,只有类组件才......
  • TypeScript 官方文档(中文版)All In One
    TypeScript官方文档(中文版)AllInOneTypeScripthandbook/TypeScript手册https://www.typescriptlang.org/zh/docs/handbook/intro.htmlhttps://www.typescriptla......
  • 那些你不知道的Typescript面试题
    1.面试官:说说你对TypeScript中类的理解?应用场景?一、是什么类(Class)是面向对象程序设计(OOP,Object-OrientedProgramming)实现信息封装的基础类是一种用户定义的引用数据类型,......
  • Typescript类型体操 - Fill
    题目中文Fill是javascript中常用的方法,现在让我实现类型版本的FillFill<T,N,Start?,End?>,正如你看到的那样,Fill接受四个泛型参数,其中T和N是必填参数,......
  • Typescript类型体操 - Chunk
    题目中文你知道lodash吗?lodash中有一个非常实用的方法Chunk,让我们实现它吧.Chunk<T,N>接受两个泛型参数,其中T是tuple类型,N是大于1的数字typeexp1=......
  • Typescript类型体操 - GreaterThan
    题目中文在本挑战中,你需要实现GreaterThan<T,U>,它的作用像T>U你不需要考虑负数示例:GreaterThan<2,1>//shouldbetrueGreaterThan<......
  • [Typescript] 66. Medium - IsTuple
    Implementatype IsTuple,whichtakesaninputtype T andreturnswhether T istupletype.Forexample:typecase1=IsTuple<[number]>//truetypecase2......
  • [Typescript] 65. Medium - Zip
    InThisChallenge,Youshouldimplementatype Zip<T,U>,TandUmustbe Tupletypeexp=Zip<[1,2],[true,false]>//expectedtobe[[1,true],[2,false......