Given an interface (from an existing .d.ts file that can\'t be changed):
interface Foo {
[key: string]: any;
bar(): void;
}
Is there a
There is a way, requiring TypeScript 2.8's Conditional Types.
It is based on the fact that 'a' extends string but string doesn't extends 'a'
interface Foo {
[key: string]: any;
bar(): void;
}
type KnownKeys = {
[K in keyof T]: string extends K ? never : number extends K ? never : K
} extends { [_ in keyof T]: infer U } ? U : never;
type FooWithOnlyBar = Pick>;
You can make a generic out of that:
// Generic !!!
type RequiredOnly> = Pick>;
type FooWithOnlyBar = RequiredOnly;
For an explanation of why exactly KnownKeys works, see the following answer:
https://stackoverflow.com/a/51955852/2115619