Typescript Required、Partial与Pick的使用

interface User {
    name: string;
    id: number;
    age: number;
}

// Required提取全部属性
type reqUser = Required<User>
/**
 type reqUser = {
    name: string;
    id: number;
    age: number;
}
 */

// Partial生成可选属性
type partUser = Partial<User>
/**
 type partUser = {
    name?: string;
    id?: number;
    age?: number;
}
*/

// Pick提取部分属性
type pickUser2 = Pick<User,'name'|'id'>
/**
     type pickUser2 = {
        name: string;
        id: number;
    }
*/

type pickUser = Pick<User, keyof {
    name: string
}>
/**
 type pickUser = {
    name: string;
}
 */
posted @ 2022-04-02 17:31  IslandZzzz  阅读(166)  评论(0编辑  收藏  举报