Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a type that refer to a type of an interface's field?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But I wonder how to do that.
Thanks

>Solution :

There is another simple syntax:

type OptionName = SomeOption['name']; 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading