假设我有一个类型
type Z = {a: number} | {} | {b: boolean} | {c: string} | ...;
如何获得相同但没有的商品{}
?
type Y = Exclude<Z, {}>;
in结果为Y = never
,因为所有变体都可分配给{}
,所以将其排除在外。
jcalz的答案非常棒,据我了解,我认为也许有一种基于jcalz的方法,可以排除完全相同的类型。
type ExcludeExact<T, U> = T extends any ? U extends T ? (T extends U ? never : T) : T : never;
这可以根据需要工作:
type Y = ExcludeExact<Z, {}>
// type Y = { a: number } | { b: boolean } | { c: string }
并以这种方式工作:
type Y = ExcludeExact<Z, {a: number}>
// type Y = { } | { b: boolean } | { c: string }
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句