Currently, I am in the process of developing a type definition set that functions on a user-provided type representing the model of their "state".
One crucial task I must accomplish is narrowing down the types of their model as I generate new types that will be utilized and exposed to them.
I have encountered difficulties in narrowing down properties on an object that are index signatures.
For instance:
/**
* Selects only the keys of a specific type
* Extracted from typelevel-ts
*/
type KeysOfType<A extends object, B> = {
[K in keyof A]-?: A[K] extends B ? K : never;
}[keyof A];
interface Address {
street: string;
postCode: string;
}
interface Person {
// My goal is to narrow down to this property
favouriteNumbers: {
[id: string]: number;
};
name: string;
address: Address;
}
type PersonIndexSignatures = Pick<
Person,
KeysOfType<Person, { [key: string]: any; }>
>;
type PersonIndexSignaturesKeys = keyof PersonIndexSignatures;
// "favouriteNumbers" | "address"
As illustrated above, my attempt to narrow down Person
based on { [key: string]: any; }
results in both the favouriteNumbers
and address
keys.
Are there any techniques I can utilize to narrow the type down to just favouriteNumbers
?