是否可以从联合中排除一个空对象?

Is it possible to Exclude an empty object from a union?

我有两种类型的联合,其中一种是空对象。

type U = {} | { a: number } // | { b: string } | { c: boolean } ....

我想从联合中排除空对象,但是 Exclude 没有帮助

type A = Exclude<U, {}>
// A = never

我试过使用 as const 但结果是一样的

const empty = {} as const
type Empty = typeof empty
type U = Empty | { a: number }
type A = Exclude<U, Empty>
//type A = never

额外的讽刺是排除其他属性很简单

  type B = Exclude<U, { a: number }>
  // type B = {}

TS Playground

那么是否可以从联合中的其他接口中排除一个空接口?

根据条件类型的文档 here,您实际上可以根据某些条件分配类型。

T extends U ? X : Y

因此对于上述问题,您可以使用 keyof 关键字从对象中提取键。当没有找到任何键时,类型永远不会如此,我们可以检查 keyof object 是否从不扩展,即

 keyof K extends never

下面结合条件输入;

const empty = {} as const
type Empty = typeof empty

type NoEmpty<K> = keyof K extends never ? never : K;

type C = NoEmpty<Empty>;

type U = NoEmpty<Empty> | NoEmpty<{ a: number }>

你终于可以看到 U 的类型是非空对象,即排除空对象。检查这个 playground

回答我自己的问题..

如果您使用@lukasgeiter 的AtLeastOne,请在此处回答:Exclude empty object from Partial type

您可以执行以下操作:

type AtLeastOne<T, U = {[K in keyof T]: Pick<T, K> }> = Partial<T> & U[keyof U];
    
type ExcludeEmpty<T> = T extends AtLeastOne<T> ? T : never; 
    
type U = {} | { a: number } | { b: string }
    
type Foo = ExcludeEmpty<U> // { a: number } | { b: string }

TSplayground