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?
CodePudding user response:
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
}