我想声明一个这样的类型:
interface DependData {[key: string]: string};
Run Code Online (Sandbox Code Playgroud)
但是有这样的错误:
Statements are not allowed in ambient contexts
Run Code Online (Sandbox Code Playgroud)
您描述的错误消息发生在定义文件中.
要使其工作,您需要使用正确的语法并删除接口声明末尾的分号:
interface DependData {
[key: string]: string;
}
Run Code Online (Sandbox Code Playgroud)
我不擅长打字稿,当我深入研究我们的代码库时,我发现以下方法也是有效的,并且可以更加健壮,因为否则您将无法使用非字符串作为键。
export enum SORT_TYPES {
DISPLAY_NAME_ASC = 'Patient: A-Z',
DISPLAY_NAME_DESC = 'Patient: Z-A',
}
export const SORT_ORDERS: Map<SORT_TYPES, String[]> = new Map([
[SORT_TYPES.DISPLAY_NAME_ASC, ['display_name', 'ASC']],
[SORT_TYPES.DISPLAY_NAME_DESC, ['display_name', 'DESC']],
])
Run Code Online (Sandbox Code Playgroud)
所以这里Map使用了一个类型,键类型就变成了SORT_TYPES而不是字符串。
| 归档时间: |
|
| 查看次数: |
13595 次 |
| 最近记录: |