2021年1月25日星期一

typescript template literal as interface key

Lets say that I want to create a object contain multiple items in typescript as below:

const obj: Items = {    item1: 'foo',    item2: 'bar',    item3: 'baz',  }  

How should I declare my Items type so that it's compatible with any number of items? I tried the following with template literals from Typescript 4.1 and it doesn't seem to work:

interface Items {    [P: `array${number}`]: any;  }  

Is it possible to declare a type like this?

https://stackoverflow.com/questions/65878880/typescript-template-literal-as-interface-key January 25, 2021 at 12:11PM

没有评论:

发表评论