gpt4 book ai didi

typescript - 我怎样才能有一个使用对象作为键的字典?

转载 作者:搜寻专家 更新时间:2023-10-30 21:01:15 25 4
gpt4 key购买 nike

我尝试使用对象作为键的字典:

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”的精彩库。请找到以下链接: typescript collections

您只需要使用以下命令安装它:

npm install typescript-collections

它还包含所有必要的定义,只是为了让代码体验更好。

关于typescript - 我怎样才能有一个使用对象作为键的字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37614661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com