Skip to main content

TS 高级技巧(Pick,Partial, Required)

Pick

interface Todo {
sing: string;
run: boolean;
wait?: boolean
}
// 包含键
Pick type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
const name: Pick<Todo, 'sing' | 'run'> = {
sing: 'sing',
}
const name2: Pick<Todo, 'sing' & 'run'> = {
sing: 'sing',
run: true,
}
// 全部必须
type Required<T> = {
[P in keyof T]-?: T[P];
};
const name3: Required<Todo> = {
sing: 'sing',
run:true,
wait: true
}
// 全部可选
type Partial<T> = {
[P in keyof T]?: T[P];
};
const name4: Partial<Todo> = {
sing: 'partial',
}