[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 a new type by transforming all properties of an existing type according to a given transformation function. In this lesson, we'll cover mapped types like Readonly<T> or Partial<T> that ship with the TypeScript compiler, and we'll also explore how to create our own type transformations.
There are few mapped types built-in:
/**
* Make all properties in T optional
*/
type Partial<T> = {
[P in keyof T]?: T[P];
}; /**
* Make all properties in T readonly
*/
type Readonly<T> = {
readonly [P in keyof T]: T[P];
}; /**
* From T pick a set of properties K
*/
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
}; /**
* Construct a type with a set of properties K of type T
*/
type Record<K extends string, T> = {
[P in K]: T;
};
Take readonly as an example, the output is like this:
interface Point {
x: number;
y: number;
}
ReadonlyPoint = Readonly<Point>;
type ReadonlyPoint = {
readonly x: number;
readonly y: number;
}
So for each props in Point, we append 'readonly' type for it.
The way type resolve:
interface Point {
x: number;
y: number;
}
// From
type ReadonlyPoint = {
readonly [P in keyof Point]: Point[P]
}
type ReadonlyPoint = {
readonly [P in "x" | "y"]: Point[P]
}
type ReadonlyPoint = {
readonly x: Point["x"];
readonly y: Point["y"];
}
// To
type ReadonlyPoint = {
readonly x: number
readonly y: number;
}
The same process can be done with Partial type:
type PartialPoint = Partial<Point>; // From
type PartialPoint = {
[P in keyof T]?: T[P];
} type PartialPoint = {
[P in keyof Point]?: Point[P];
} type PartialPoint = {
[P in "x" | "y"]?: Point[P];
} type PartialPoint = {
x?: Point["x"];
y?: Point["y"];
} // To
type PartialPoint = {
x?: number;
y?: number;
}
We can also write Nullable type by ourselves:
type Nullable<T> = {
[P in keyof T]: T[P] | null
}
For each Prop fo Type T, it can be its value or null.
We can also combine different type together:
type Nullable<T> = {
[P in keyof T]: T[P] | null
}
type Stringify<T> = {
[P in keyof T]: string
}
type NullablePoint = Nullable<Point>
type PartialNullablePoint = Partial<Nullable<Stringify<Point>>>
[TypeScript] Transform Existing Types Using Mapped Types in TypeScript的更多相关文章
- [TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5
Typescript 2.5 adds JSDoc type assertion support for javascript file via ts-check service. First of ...
- [TypeScript] Model Alternatives with Discriminated Union Types in TypeScript
TypeScript’s discriminated union types (aka tagged union types) allow you to model a finite set of a ...
- [TypeScript] Union Types and Type Aliases in TypeScript
Sometimes we want our function arguments to be able to accept more than 1 type; e.g. a string or an ...
- [TypeScript] Dynamically Allocate Function Types with Conditional Types in TypeScript
Conditional types take generics one step further and allow you to test for a specific condition, bas ...
- [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
TypeScript has 'interface' and 'type', so when to use which? interface hasName { firstName: string; ...
- [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 ...
- Java中的Union Types和Intersection Types
前言 Union Type和Intersection Type都是将多个类型结合起来的一个等价的"类型",它们并非是实际存在的类型. Union Type Union type(联 ...
- Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.
代码如下: var query = from s in db.LoginUserServices join ss in db.Services on s.ServiceType equals ss.C ...
- [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 ...
随机推荐
- tabsGif
tabsGif
- python基础一 day5 复习
bytes类型默认编码方式是utf-8和gbk,反正不是unicode strn内部的编码方式是Unicode range相当于只有一次,第一次之后相当于一个有序列表.range顾头不 ...
- CPP-基础:关于私有成员的访问
a.C++的类的成员函数中,允许直接访问该类的对象的私有成员变量. b.在类的成员函数中可以访问同类型实例的私有变量. c.拷贝构造函数里,可以直接访问另外一个同类对象(引用)的私有成员. d.类的成 ...
- Linux内核网络数据包处理流程
Linux内核网络数据包处理流程 from kernel-4.9: 0. Linux内核网络数据包处理流程 - 网络硬件 网卡工作在物理层和数据链路层,主要由PHY/MAC芯片.Tx/Rx FIFO. ...
- [Usaco2009 Nov]lights
题目描述: 给出$n$,$m$,表示有$n$盏灯和$m$条奇怪的电线,按下电线一段的灯后另一端会有影响. 求最少按几次. 题解: 高消解异或方程组,得到一堆自由元后搜索自由元状态,然后不断更新答案. ...
- 什么是PHP中的heredoc和nowdoc
heredoc结构类似于双引号字符串,nowdoc结构是类似于单引号字符串的.nowdoc结构很像heredoc结构,但是 nowdoc不进行解析操作. 这种结构很适合用在不需要进行转义的PHP代码和 ...
- cobbler 无人值守-安装
环境准备 准备两台主机,如centos6和centos7 centos7当作server服务器 关闭selinux 关闭防火墙 安装 cobbler包光盘里是没有的,要配置epel源,这里就说怎么配置 ...
- CSS--基础块级元素与内联元素
在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素.在HTML和XHTML中,块级元素不能继承自行内元素(即不能嵌套在行内元素),<p&g ...
- Python 3.52官方文档翻译 http://usyiyi.cn/translate/python_352/library/index.html 必看!
Python 3.52官方文档翻译 http://usyiyi.cn/translate/python_352/library/index.html 觉得好的麻烦点下推荐!谢谢!
- tomcat——大致简介和执行过程
jsp简介 JSP: JAVA Server Page 使用JAVA语言编写的一种在服务器运行的动态页面 JSP = JAVA + HTML JSP 的执行过程 1: 翻译阶段 把JSP源文件翻译成 ...