首页IT科技typescript(TypeScript 之 Type)

typescript(TypeScript 之 Type)

时间2025-05-01 09:28:52分类IT科技浏览3616
导读:Type 描述:全称叫做 类型别名,为类型字面量提供名称。比 Interface 支持更丰富的类型系统特性。 Type 与 Interface 区别 Interface 只能描述对象的形状,Type 不止 Interface 能多次声明进行扩展,Type 不行...

Type

描述:全称叫做 类型别名          ,为类型字面量提供名称            。比 Interface 支持更丰富的类型系统特性                。 Type 与 Interface 区别 Interface 只能描述对象的形状                 ,Type 不止 Interface 能多次声明进行扩展      ,Type 不行 在性能方面        ,Type 接口检查能够更快 特性

和变量类似                ,可以在不同的作用域中创建具有相同的名称     。

TypeScript 内置了许多全局类型         ,将帮助你在类型系统完成常用的任务         。

对象字面量语法( Object Literal Syntax )

这些对象字面量的语法和 Interface 的没有区别      ,详情可阅读我另一篇 Interface 随笔                 。https://www.cnblogs.com/lxrNote/p/16953606.html

原始类型(Primitive Type)
type SanitizedIput = string type MissingNo = 404

元组类型(Tuple Type)

元组是知道明确下标的特殊数组
type Data = [ location: Location, timestamp: string ]

联合类型(Union Type)

联合类型是描述众多类型之一
type Size = "small" | "medium" | "large"

交叉类型(Intersection Types)

一种扩展/合并的方法
type local = { x: number } & { y: number } // local {x:number;y:number}

索引类型(Type Indexing)

如果其它类型别名是引用类型                ,可以通过索引获取其值类型

type Res = { data: { x: string } } type Content = Res["data"] // Conent {x:string}

类型来自值(Type from Value)

重用通过 typeof 运算符取出值在JavaScript 运行时的类型
const data = { x: 123 } type Content2 = typeof data // Content2 = { x: number }

类型来自方法返回值(Type from Func Return)

重用方法的返回值作为类型            ,ReturnType 是内置全局类型
const createFixtures = ()=>{ return 123} type Fixtures = ReturnType<typeof createFixtures> // Fixtures = numer

类型来自模块(Type from Module)

const data: import("./data").data

没看懂   ,知道的朋友指导下       。      。                  。          。   。                  。

映射类型(Mapped Types)

type Artist = {name: string,bio: string} type Subscriber<Type> = { // 循环遍历泛型参数 Type 的每一个字段 [Property in keyof Type]: (nv: Type[Property]) => void //设置类型为一个函数                ,原始类型为参数 } type ArtisSub = Subscriber<Artist> // { name: (nv: string)=>void, bio: (nv:string)=>void }

条件类型(Conditional Types)

在类型系统中充当 if 语句               ,通过泛型创建,通常用于减少联合类型中的选项数量             。
type HasFourLegs<Animal> = Animal extends { legs: 4 } ? Animal : never type Bird = { cry: 唧唧喳喳, legs: 2 } type Dog = { cry: 汪汪汪, legs: 4 } type Ant = { cry: ..., legs: 6 } type Wolf = { cry: 嗷呜~, legs: 4 } type Animal = Bird | Dog | Ant | Wolf type FourLegs = HasFourLegs<Animal> // FourLegs = Dog | Wolf

模板联合类型(Template Union Types)

字符串模板可以用来组合和操纵类型系统中的文本
type SupportedLangs = "en" | "pt" | "zh"; type FooterLocaleIDs = "header" | "footer"; type AllLocaleIDs = `${SupportedLangs}_${FooterLocaleIDs}_id`; // "en_header_id" | "en_footer_id" | "pt_header_id" | "pt_footer_id" | "zh_header_id" | "zh_footer_id"

感谢观看             ,欢迎互相讨论与指导                  ,以下是参考资料链接?

https://www.typescriptlang.org/static/TypeScript%20Types-ae199d69aeecf7d4a2704a528d0fd3f9.png

声明:本站所有文章   ,如无特殊说明或标注          ,均为本站原创发布。任何个人或组织                 ,在未征得本站同意时      ,禁止复制          、盗用                 、采集      、发布本站内容到任何网站        、书籍等各类媒体平台               。如若本站内容侵犯了原著者的合法权益        ,可联系我们进行处理                。

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
thinkpad8装win10系统(thinkpad预装Windows 8系统改装Windows 7系统的操作方法) 安装操作系统的步骤方法(安装操作系统:GHOST备份还原图文教程)