In TypeScript, it is currently possible to get the union type of all the "keys" of an interface, something like:
interface foo {
name: string;
age: number;
}
function onlyTakeFooKeys(key: keyof foo) {
// do stuff with key
}
And the compiler will enforce that onlyTakeFooKeys
can only be called with either name
or age
.
My use case is that I need to get a union type of the all the types of an interface/object's member. In the given interface foo
example, I'm expecting something like this:
some_operator foo; //=> string | number
Is this currently achievable?
Assuming the interfaces have arbitrary number of members which makes hard-coding impossible. And that this needs to be done at compile time.