How to create a type that refer to a type of an interface’s fields?
Here is what I want to achieve:
interface SomeOption {
readonly name: string;
readonly value: number;
}
type OptionName = typeof SomeOption.name; // <-- this doesn't work of course
const name: OptionName = 'foo'; // ok
const name: OptionName = 5; // error
I assume that SomeOption might be changed in the future, so I want to say that my variables should be the same type as SomeOption.name, not just string.
But I wonder how to do that.
Thanks
>Solution :
There is another simple syntax:
type OptionName = SomeOption['name'];