typescript中的Omit排除类型及Pick取想要的属性

Omit 的使用:排除类型

type OmitUser = {
    name: string,
    age: number,
    sex:string
}
type newOmit = Omit

// 定义一个对象并将其类型设置为 newOmit
const example: newOmit = {
    name: "John",
    age: 30
};

console.log(' Omit 的使用:排除类型 ', example);

pick 使用 从一个类型中取出几个想要的属性

// pick 使用 从一个类型中取出几个想要的属性
interface ITest{
    type: string,
    text:string
}
type TTest = Pick
let pickRes: TTest = {
    text:'测试只取text'
}
console.log('Pick只取想要的属性',pickRes);

你可能感兴趣的:(typescript,javascript,前端)