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

complex types in typescript

I need to describe in my interface a string with indexes also in the end of string can be work or crash:

export interface Test {
    date: string,
   'stat.conv[index: number].key[index: number].(work || crash)': Test2[]
}

Is this possible in typescript?

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

>Solution :

You can use a template literal type to define the keys:

export interface Test {
    date: string;
   [key: `stat.conv[${number}].key[${number}].${'work'| 'crash'}`]: any;
}

const t: Test = {
  date: '',
  'stat.conv[0].key[0].work': '', // OK
  'stat.conv[1].key[1].crash': '', // OK
  'stat.conv[x].key[y].work': '', // Error
  'stat.conv[0].key[0].nothing': '' // Error
}
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