For example, how can we extract only the keys that have string types? Here is what I have tried:
type StringEntriesOnly<T> = {
[K in keyof T]: T[K] extends string ? T[K] : never;
}
type SomeType = {
aString: string;
aNumber: number;
}
const v1: StringEntriesOnly<SomeType> = {
aString: 'Hello',
aNumber: 10, // This is a compiler error. Good.
}
type StringKeys = keyof StringEntriesOnly<SomeType>;
const v2: StringKeys = 'aNumber'; // How can we make the compiler complain here?
In the above example, I would like StringKeys
to allow only 'aString'.