Omit<T, K>工具类型与Pick<T, K>工具类型是互补的,它能够从已有对象类型中剔除给定的属性,然后构建出一个新的对象类型。“Omit<T, K>”
工具类型中的类型参数T
表示源对象类型,类型参数K
提供了待剔除的属性名类型,但它可以为对象类型T
中不存在的属性。示例如下:
interface A {
x: number;
y: number;
}
type T0 = Omit<A, 'x'>; // { y: number }
type T1 = Omit<A, 'y'>; // { x: number }
type T2 = Omit<A, 'x' | 'y'>; // { }
type T3 = Omit<A, 'z'>; // { x: number; y: number }
const a: T0 = { y: 40 };
const b: T1 = { x: 50 };
const c: T2 = { x: 60 };
console.log(a)
console.log(b)
console.log(c)
输出结果:
酷客网相关文章:
评论前必须登录!
注册