[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 ...
随机推荐
- error: version in "./docker-compose.yml" is unsupported
#sudo rm /usr/bin/docker-compose #curl -L https://github.com/docker/compose/releases/download/1.20.0 ...
- Java会话(session)管理
会话概述 什么是会话 简单的理解:用户打开浏览器,点击多个超链接,访问Web服务器上多个资源,然后关闭浏览器,整个过程称之为一次会话. 需要解决的问题 每个用户在使用浏览器与服务器会话的过程中,会产生 ...
- windows图标变空白解决方案
背景 对磁盘软件路径进行规整,把磁盘不同类型软件进行分类存储后,部分软件在windows桌面为空白,有些在start menu不为空白但是发送至桌面后却变成空白 解决方法 工具:search ever ...
- 模拟、字符串--P1042 乒乓球 题解
P1042 乒乓球 字符串string的基本使用 #include <iostream> #include <algorithm> #include <map> # ...
- luogu P1866 编号
题目描述 太郎有N只兔子,现在为了方便识别它们,太郎要给他们编号.兔子们向太郎表达了它们对号码的喜好,每个兔子i想要一个整数,介于1和Maxnumber[i]之间(包括1和Maxnumber[i]). ...
- 简单DP内容
1. 最长上升子序列 [题目描述] 给定N个数,求这N个数的最长上升子序列的长度. [样例输入] 7 2 5 3 4 1 7 6 [样例输出] 4 第一种解法:时间复杂度O(n^2), 状态设计:DP ...
- qt 窗体间通信
利用qt的信号和槽,可以完成窗体间的通信,下面列出父子窗口利用信号和槽的相关代码. parent窗口: //parent.h #ifndef PARENT_H #define PARENT_H #in ...
- ThinkPHP foreach标签
$optionvalue = array( 'MSGTYPE_TEXT'=>'文本消息', 'MSGTYPE_EVENT_SCAN'=>'扫描事件', 'MSGTYPE_EVENT_sub ...
- 使用 ES (elasticsearch) 搜索中文
1.创建索引 curl -XPUT http://172.16.125.139:9200/ques2.创建索引类型 curl -XPOST http://172.16.125.139:9200/que ...
- ArrayList集合的遍历
ArrayLIstDemo3.java import java.util.ArrayList; public class ArrayListDemo3 { public static void mai ...