我尝试使用对象作为键的字典:
api: {[key: string]: string} = {
getItems: 'api/v1/items/all'
};
如果我尝试使用它var urlpath = api.getItems;我明白了:
Property ‘getItems’ does not exist on type ‘{ [key: string]: string;`
}.
如果我将密钥的类型更改为任何{[key:any]:string},我会得到:
An index signature parameter type must be ‘string’ or ‘number’.
我可以像这样使用它,但那不是我想要的:
var urlpath = api['getItems'];
如何使用对象作为键的字典?
最佳答案 您可以使用@basarat创建的精美图书馆并命名为“typescript-collections”.请找到以下链接:
您需要的是使用以下命令安装它:
npm install typescript-collections
它还包含所有必要的定义,以使代码体验更好.