TypeScript 之 Type
Type
描述:全称叫做 '类型别名',为类型字面量提供名称。比 Interface 支持更丰富的类型系统特性。
Type 与 Interface 区别
- Interface 只能描述对象的形状,Type 不止
- Interface 能多次声明进行扩展,Type 不行
- 在性能方面,Type 接口检查能够更快
特性
和变量类似,可以在不同的作用域中创建具有相同的名称。
TypeScript 内置了许多全局类型,将帮助你在类型系统完成常用的任务。
对象字面量语法( Object Literal Syntax )
type JsonRsponse = {
version: number;
outOfStock?: boolean; // 可选属性
readonly body: string; // 只读属性
/** In bytes */
payloadSize: number; // 编辑器注释提示
update: (retryTimes: number) => void; // 箭头函数方法
update2(retryTimes: number): void; // 方法
(): JsonRsponse; // 函数
[key: string]: number; // 接受字符串索引,值为number
new (s: string): JSONResponse; // 构造函数
}
这些对象字面量的语法和 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)
const data = { x: 123 }
type Content2 = typeof data
// Content2 = { x: number }
类型来自方法返回值(Type from Func Return)
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)
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
TypeScript 之 Type的更多相关文章
- TypeScript & as & Type Assertion
TypeScript & as & Type Assertion Type Assertion (as) That is not vanilla JavaScript, it is T ...
- [TypeScript] Work with DOM Elements in TypeScript using Type Assertions
The DOM can be a bit tricky when it comes to typing. You never really know exactly what you're going ...
- [TypeScript] Use the TypeScript "unknown" type to avoid runtime errors
The "any" type can be very useful, especially when adding types to an existing JavaScript ...
- [TypeScript] Increase TypeScript's type safety with noImplicitAny
TypeScript tries to infer as much about your code as it can. But sometimes there really is not enoug ...
- [TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers
Using the optional “+” sign together with mapped type modifiers, we can create more explicit and rea ...
- [TypeScript] Represent Non-Primitive Types with TypeScript’s object Type
ypeScript 2.2 introduced the object, a type that represents any non-primitive type. It can be used t ...
- (译文)开始学习Webpack-应用TypeScript,配置热加载和Source Map
项目初始化:采用TypeScript 我们的版本是: $ node --version v8.5.0 $ npm --version 5.5.1 npm版本升级了,因为npm最近带来了新特性,本地会生 ...
- 6、什么是TypeScript、TypeScript的安装、转换为.js文件
1.什么是TypeScript (本人用自己的理解梳理了一下,不代表官方意见) TypeScript:Type+ECMAScript6 TypeScript是一种预处理编程语言,遵循es6标准规范,在 ...
- [TypeScript] Transform Existing Types Using Mapped Types in TypeScript
Mapped types are a powerful and unique feature of TypeScript's type system. They allow you to create ...
- [TypeScript] Restrict null and undefined via Non-Nullable-Types in TypeScript
This lesson introduces the --strictNullChecks compiler option and explains how non-nullable types di ...
随机推荐
- Prometheus 通过 consul 分布式集群实现自动服务发现
转载自:https://cloud.tencent.com/developer/article/1611091 1.Consul 介绍 Consul 是基于 GO 语言开发的开源工具,主要面向分布式, ...
- ExecutorService、Callable、Future实现有返回结果的多线程原理解析
原创/朱季谦 在并发多线程场景下,存在需要获取各线程的异步执行结果,这时,就可以通过ExecutorService线程池结合Callable.Future来实现. 我们先来写一个简单的例子-- pub ...
- 4_Spring
一. Spring Spring的基本组成: 1.最完善的轻量级核心框架. 2.通用的事务管理抽象层. 3.JDBC抽象层. 4.集成了Toplink, Hibernate, JDO, and iBA ...
- RabbitMQ原理和架构图解(附6大工作模式)
为什么要使用RabbitMQ? 1.解耦 系统A在代码中直接调用系统B和系统C的代码,如果将来D系统接入,系统A还需要修改代码,过于麻烦. 2.异步 将消息写入消息队列,非必要的业务逻辑以异步的方式运 ...
- 洛谷P4304 TJOI2013 攻击装置 (二分图匹配)
题目大意:一个矩阵,一些点被拿掉,在棋盘上马走日,马之间不能落在同一点,求最多放几匹马. 采用对矩阵黑白染色,画个图可以发现:马可以走到的位置和他所处的位置颜色不同,将马和他可以走到的位置连边,最多可 ...
- java中实现File文件的重命名(renameTo)、将文件移动到其他目录下、文件的复制(copy)、目录和文件的组合(更加灵活方便)
欢迎加入刚建立的社区:http://t.csdn.cn/Q52km 加入社区的好处: 1.专栏更加明确.便于学习 2.覆盖的知识点更多.便于发散学习 3.大家共同学习进步 3.不定时的发现金红包(不多 ...
- 在vue中_this和this的区别
_this只是一个变量名,this代表父函数,如果在子函数还用this,this的指 向就变成子函数了,_this就是用来存储指向的 普通函数中的this表示调用此函数时的对象,箭头函数里面的this ...
- 实现etcd服务注册与发现
转载自:实现etcd服务注册与发现 0.1.目录结构 . ├── api │ └── main.go ├── common │ └── common.go ├── docker-compose ...
- 学习ASP.NET Core Blazor编程系列八——数据校验
学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...
- GIT入门与Gitee的使用
一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 工作原理 / 流程: Workspace:工作区Index / Stage:暂存区Repository:仓库区(或本地仓库)Remo ...