首页 > 其他分享 >[Typescript 5] infer Constraints

[Typescript 5] infer Constraints

时间:2024-01-29 23:55:26浏览次数:30  
标签:Typescript const success let GetFirstStringIshElement extends infer Constraints

Since typescript 5, we are able to add constraints over infer.

Following code doesn't apply constraints, so the inferred element could be stringand number

type GetFirstStringIshElement<T> = T extends readonly [
  infer S,
  ..._:any[]
] ? S : never

const t1 = ["success", 2, 1, 4] as const
//     ^?
const t2 = [4, 54, 5] as const
//     ^?
let firstT1: GetFirstStringIshElement<typeof t1>
//   ^? success
let firstT2: GetFirstStringIshElement<typeof t2>
//   ^? 4

 

Now we want inferred element only being string

Then what we can do is:

type GetFirstStringIshElement<T> = T extends readonly [
  infer S extends string,
  ..._:any[]
] ? S : never

const t1 = ["success", 2, 1, 4] as const
//     ^?
const t2 = [4, 54, 5] as const
//     ^?
let firstT1: GetFirstStringIshElement<typeof t1>
//   ^? success
let firstT2: GetFirstStringIshElement<typeof t2>
//   ^? never

 

标签:Typescript,const,success,let,GetFirstStringIshElement,extends,infer,Constraints
From: https://www.cnblogs.com/Answer1215/p/17995627

相关文章

  • [Typescript] Native ES module support
    Node.js13.2.0introducedsupportfornativeESmodules.Thismeansyoucannativelyruncodecontainingthinglike import{Foo}from'bar',usetop-level await andmore!Howtounambiguouslyindicatewhichtypeofmoduleyou’reauthoringFil......
  • [转]TypeScript类型编程中的extends和infer示例解析
    转自;https://www.jb51.net/javascript/294261vgi.htm TypeScript类型编程中的extends和infer示例解析 −目录引文extends条件判断约束参数类型约束infer推导的局部变量类型类型转换infer组合使用ReturnTypeParameters引文在刚接触TypeScript的时候,......
  • [Typescript] Handle CommonJS import in Typescript
    Let'ssayweneedtousealibrarywithcommonJScode.classMelon{cutIntoSlices(){}}module.exports=MelonThenwewanttoimportthisinsideourTypescriptproject:import*asmelonNamespacefrom"./melon"//typescriptdoesn......
  • abc152F - Tree and Constraints
    abc152F-TreeandConstraints题意:给定一棵树,要求对每条边染成黑色或者白色,其中有m个限制,第i个限制形如ai,bi,表示ai到bi的路径上至少有一条黑色边,求方案数。看到数据第一反应是状压,但是好像没办法搞。于是考虑容斥,能想到容斥的话就差不多做完了,每次标记一下两个点和他们的lc......
  • 使用命令行方式搭建uni-app + Vue3 + Typescript + Pinia + Vite + Tailwind CSS + uv
    使用命令行方式搭建uni-app+Vue3+Typescript+Pinia+Vite+TailwindCSS+uv-ui开发脚手架项目代码以上传至码云,项目地址:gitee.com/breezefaith…目录一、前言近日心血来潮想做一个开源项目,目标是做一款可以适配多端、功能完备的模板工程,包含后台管理系统和前台......
  • OpenHarmony—TypeScript到ArkTS约束说明
    对象的属性名必须是合法的标识符规则:arkts-identifiers-as-prop-names级别:错误在ArkTS中,对象的属性名不能为数字或字符串。通过属性名访问类的属性,通过数值索引访问数组元素。TypeScriptvarx={'name':'x',2:'3'};console.log(x['name']);console.log(x[2]);ArkT......
  • 在TypeScript项目中搭配Axios封装后端接口调用
    前言本来是想发next.js开发笔记的,结果发现里面涉及了太多东西,还是拆分出来发吧~本文记录一下在TypeScript项目里封装axios的过程,之前在开发StarBlog-Admin的时候已经做了一次封装,不过那时是JavaScript,跟TypeScript还是有些区别的。另外我在跟着next.js文档开发的......
  • [Typescript] Resolving the Block-scoped Variable Error in TypeScript (moduleDete
    constNAME="Matt";TypeScriptistellinguswecan'tredeclarethe name variablebecauseithasalreadybeendeclaredinsideof lib.dom.d.ts.The index.ts fileisbeingdetectedasaglobalscriptratherthanamodule.Thisisbecause,by......
  • [Typescript] Show the error close to where it's causes
    Examplecode:constroutingConfig={routes:[{path:"home",component:"HomeComponent",},{path:"about",component:12,},{path:"contact",componen......
  • TypeScript 实用技巧(下)
    第六部分:杂项原文:exploringjs.com/tackling-ts/pt_miscellaneous.html译者:飞龙协议:CCBY-NC-SA4.0下一步:23 使用类型进行计算的概述二十三、类型计算概述原文:exploringjs.com/tackling-ts/ch_computing-with-types-overview.html译者:飞龙协议:CCBY-NC-SA4.023.......