Is there a way to determine if a typescript interface contains at least one property at compile time without knowing the property names?
For example, with the interfaces Cat and Dog defined as follows:
export type Cat = {};
export type Dog = { barking: boolean };
I require a conditional type HasAnyProperties<T>
to provide the following results:
type catHasProperties = HasAnyProperties<Cat>; // false
type dogHasProperties = HasAnyProperties<Dog>; // true
I specifically do not want to:
- Use
Object.keys(obj).length
- Enforce or require known properties. I only want to check.
This might seem like an unusual request, but I am actually trying to filter a type and then determine if anything remains. This information is then used to potentially add a new property on a mapped type.
Various attempts have failed so far:
// This always returns true
type HasAnyProperties<T> = T extends { [key: string]: any } ? true : false;
If possible, the solution may resemble something like RequireAtLeastOne.