Wong Jia Hau
Wong Jia Hau

Reputation: 3069

How to create a type function that checks whether a type is empty object in TypeScript?

Is it possible to create a type function IsEmpty such that:

type a = IsEmpty<{}> // true
type b = IsEmpty<{x: 1}> // never

Upvotes: 1

Views: 42

Answers (1)

钵钵鸡实力代购
钵钵鸡实力代购

Reputation: 1042

type IsEmpty<T> = {} extends T ? true : never;

see conditional type

Upvotes: 4

Related Questions