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)

重用通过 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

TypeScript 之 Type的更多相关文章

  1. TypeScript & as & Type Assertion

    TypeScript & as & Type Assertion Type Assertion (as) That is not vanilla JavaScript, it is T ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. (译文)开始学习Webpack-应用TypeScript,配置热加载和Source Map

    项目初始化:采用TypeScript 我们的版本是: $ node --version v8.5.0 $ npm --version 5.5.1 npm版本升级了,因为npm最近带来了新特性,本地会生 ...

  8. 6、什么是TypeScript、TypeScript的安装、转换为.js文件

    1.什么是TypeScript (本人用自己的理解梳理了一下,不代表官方意见) TypeScript:Type+ECMAScript6 TypeScript是一种预处理编程语言,遵循es6标准规范,在 ...

  9. [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 ...

  10. [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 ...

随机推荐

  1. centos7使用yum方式安装redis6

    yum -y install epel-release wget make gcc-c++ cd /opt wget https://download.redis.io/releases/redis- ...

  2. PAT (Basic Level) Practice 1007 素数对猜想 分数 20

    让我们定义dn​为:dn​=pn+1​−pn​,其中pi​是第i个素数.显然有d1​=1,且对于n>1有dn​是偶数."素数对猜想"认为"存在无穷多对相邻且差为2的 ...

  3. FEX-EMU Wine踩坑记录

    FEX是一个用于在ARM64平台运行X86软件的工具,比较成熟,但是网上资料很少,所以就写了这篇FEX运行Wine踩坑记录. Termux的Fex不能用(2022年5月) 要在debian系统安装fe ...

  4. 二叉树及其三种遍历方式的实现(基于Java)

    二叉树概念: 二叉树是每个节点的度均不超过2的有序树,因此二叉树中每个节点的孩子只能是0,1或者2个,并且每个孩子都有左右之分. 位于左边的孩子称为左孩子,位于右边的孩子成为右孩子:以左孩子为根节点的 ...

  5. 重写 hashcode()真有那么简单嘛?

    万万没想到一个 hashcode() 方法,既然会引出一堆的知识盲区,简直了. 起因: 老八股:为什么重写Equals方法要重写HashCode方法. 大声告诉我为什么,闭着眼睛先把答案背出来,啥?这 ...

  6. fun无处不在 搜索结果调用其他字段的办法

    一直有人要搜索结果调用其他字段的信息 比如图片等等 一直忙没顾上 官方其实已经内置了一个内容读取器. 我们打开搜索的结果模板template\index_style\你的模板目录\search\ind ...

  7. 齐博x1会员中心菜单权限显示

    会员中心的菜单可以通过后台设置用户组权限进行显示或者是隐藏.比如商城里的菜单如下: 菜单文件是:\application\shop\member_menu.php return array( 'cms ...

  8. python django搭建一个简易博客的解析(按照文件顺序逐一讲解)

    上次讲解了一下各py文件的内容,但比较乱,所以这次整理了一个顺序版. 源代码请在http://github/Cheng0829/mysite自行下载 mysite: db.sqlite3:数据库文件. ...

  9. 云原生之旅 - 5)Kubernetes时代的包管理工具 Helm

    前言 上一篇文章 [基础设施即代码 使用 Terraform 创建 Kubernetes] 教会了你如何在Cloud上面建Kubernetes资源,那么本篇来讲一下如何在Kubernetes上面部署应 ...

  10. 一个 MySQL 隐式转换的坑,差点把服务器整崩溃了

    我是风筝,公众号「古时的风筝」,专注于 Java技术 及周边生态. 文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在里面. 本来是一个平静而美好的下午,其 ...