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

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

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

I would like to exclude the empty object from the union however Exclude is no help

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

I tried using as const but it's the same result

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

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

评论