#Check if given type is empty object

9 messages · Page 1 of 1 (latest)

warped swift
#

So, this is the example;

type ExampleType<T extends object> = Base & T === {} ? {} : { props: T }

Just like this, if T is not empty object, props is going to be defined, else we won't extends Base.

#

When I do T extends {}, it does not work

#

Check if given type is empty object

tight mica
#

@warped swift I'd be careful with this because {} doesn't really mean "empty object", it means an interface with no specified properties, but this seems to do essentially what you want?

final lionBOT
#
type HasNoKnownProperties<T> = {} extends T ? "Yes" : "No";

type Example1 = HasNoKnownProperties<{}>
//   ^? - type Example1 = "Yes"
type Example2 = HasNoKnownProperties<{ x: string }>
//   ^? - type Example2 = "No"
warped swift
#

ohh

#

I was doing the opposite, T extends {} : "No", and it is always Yes

#

amazing, I didn't know that

#

Thank you so much @tight mica