typescript 如何在打字稿中声明 Map 类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38487007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:40:29  来源:igfitidea点击:

How to declare a Map type in typescript?

typescript

提问by roger

I want to declare a type like this:

我想声明一个这样的类型:

interface DependData {[key: string]: string};

but with error like this:

但有这样的错误:

Statements are not allowed in ambient contexts

回答by David Sherret

The error message you are describing occurs in a definition file.

您描述的错误消息出现在定义文件中。

To make this work, you need to use the proper syntax and remove the semi-colon at the end of your interface declaration:

为了使这项工作,您需要使用正确的语法并删除接口声明末尾的分号:

interface DependData {
    [key: string]: string;
}