标签:lang 返回 can http pre use esc 包含 option
/**
* 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 whose keys are in the union 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 keyof any, T> = {
[P in K]: T;
};
以 typeof 格式快速创建一个类型,此类型包含一组指定的属性且都是必填。
type Coord = Record<‘x‘ | ‘y‘, number>;
// 等同于
type Coord = {
x: number;
y: number;
}
将类型定义的所有属性都修改为可选。
type Coord = Partial<Record<‘x‘ | ‘y‘, number>>;
// 等同于
type Coord = {
x?: number;
y?: number;
}
不管是从字面意思,还是定义上都很好理解:将所有属性定义为自读。
type Coord = Readonly<Record<‘x‘ | ‘y‘, number>>;
// 等同于
type Coord = {
readonly x: number;
readonly x: number;
}
// 如果进行了修改,则会报错:
const c: Coord = { x: 1, y: 1 };
c.x = 2; // Error: Cannot assign to ‘x‘ because it is a read-only property.
从类型定义的属性中,选取指定一组属性,返回一个新的类型定义。
type Coord = Record<‘x‘ | ‘y‘, number>;
type CoordX = Pick<Coord, ‘x‘>;
// 等用于
type CoordX = {
x: number;
}
本文章只是为了记录,转自掘金
作者:nachao
链接:https://juejin.cn/post/6844904066489778183
来源:掘金
Typescript中Partial, Readonly, Record, Pick
标签:lang 返回 can http pre use esc 包含 option
原文地址:https://www.cnblogs.com/yuanchao-blog/p/14392982.html