首页 > 其他分享 >TypeScript中的元组 Tuple

TypeScript中的元组 Tuple

时间:2022-10-15 11:23:32浏览次数:47  
标签:TypeScript string Tuple 元组 let 类型

元组类型

// 元组类型:表示一个已知元素数量和类型的数组,各元素的类型不必相同
let undata: [string, '男'| '女']; //已知数量是两个。类型分别是字符串和男或者女
undata =['张三', '男']  ok

//x这个类型必须一一匹配起来
let x: [string, number]; 
x = ['庞博', 18]; // OK

// 类型没有与之匹配
x = [28, '王建国']; // Error

Tuple  元组;元祖;多元组;组元;元素  读音:【ta bo】

标签:TypeScript,string,Tuple,元组,let,类型
From: https://www.cnblogs.com/IwishIcould/p/16793772.html

相关文章

  • 元组
    (1)元组可以带括号,也可以不带括号 (2)元组可以利用下标来获取元素  (3)元组不支持修改  (4)元组支持切片操作(5)count和index方法  (6)元组的拼接  (7)元组的迭代 ......
  • TypeScript Utility Types All In One
    TypeScriptUtilityTypesAllInOneAwaited<Type>Partial<Type>Required<Type>Readonly<Type>Record<Keys,Type>Pick<Type,Keys>Omit<Type,Keys>Exclude<Unio......
  • [Typescript] 50. Medium - RequiredByKeys
    Implementageneric RequiredByKeys<T,K> whichtakestwotypeargument T and K.K specifythesetofpropertiesof T thatshouldsettoberequired.Wh......
  • [Typescript] 49. Medium - PartialByKeys
    Implementageneric PartialByKeys<T,K> whichtakestwotypeargument T and K.K specifythesetofpropertiesof T thatshouldsettobeoptional.Whe......
  • [Typescript] 46. Medium - PickByType
    From T,pickasetofpropertieswhosetypeareassignableto U.ForExampletypeOnlyBoolean=PickByType<{name:stringcount:numberisReadonly:bo......
  • [Typescript] 47. Medium- StartsWith
    Implement StartsWith<T,U> whichtakestwoexactstringtypesandreturnswhether T startswith UForexampletypea=StartsWith<'abc','ac'>//expected......
  • [Typescript] 48. Medium - EndsWith
    Implement EndsWith<T,U> whichtakestwoexactstringtypesandreturnswhether T endswith UForexample:typea=EndsWith<'abc','bc'>//expectedtob......
  • [Typescript] Abstract Classes
    Youcannotcreateainstanceofabstractclass.Anabstractclassmeantobeextended.abstractclassSize{constructor(publicsizes:string[]){}......
  • [Typescript] Get class properties type in union
    Forexamplethereisaclas:exportclassModifierState{/***Returnsthemodifierstateapplicabletothekeyboardeventgiven.*......
  • Vite + Vue3 + Pinia + es6 + TypeScript 搭建项目
    vite中文参考文档:​​https://vitejs.cn/guide/#scaffolding-your-first-vite-project​​执行 npminitvite@latest步骤如下图:下载依赖npmi 启动项目:npmrundev......