pet*_*gan 7 typescript typescript-typings
我正在使用打字稿,并看到以下错误
[ts]元素隐式具有'any'类型,因为类型'{}'没有索引签名。[7017]
const store = {};
setItem: jest.fn((key, value) => {
store[key] = value.toString();
})
Run Code Online (Sandbox Code Playgroud)
我可以通过将商店设置为任何类似的方式来修复它
const store: any = {};
Run Code Online (Sandbox Code Playgroud)
但我想输入它,但不知道该怎么做。有人可以帮忙吗?
ror*_*ach 11
那么,您希望它具有哪种类型?如果只是简单的键/值对,那么就足够了:
type Dict = { [key: string]: string };
const store: Dict = {};
store['foo'] = 'bar';
Run Code Online (Sandbox Code Playgroud)
编辑(2019年6月)
Typescript还具有一个称为的内置类型Record,用于此用例- 只要您的类型不具有任何预定义的键!
const store: Record<string, string> = {};
store.foo = 'bar';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7944 次 |
| 最近记录: |