hsz*_*hsz 4 typescript typescript-typings
在我的应用程序的一些地方,我声明了一个字典类型,例如:
interface MyInterface {
data: { [key: string]: Item };
}
Run Code Online (Sandbox Code Playgroud)
TypeScript 中是否有字典/地图的任何内置速记,以获得类似于:
interface MyInterface {
data: Dict<Item>;
}
Run Code Online (Sandbox Code Playgroud)
我们可以尝试使用名为的内置打字稿高级类型 Record<K, T>
interface MyInterface {
data: Record<string, Item>;
}
Run Code Online (Sandbox Code Playgroud)
把所有东西放在一起
interface Item {
id: string;
name: string;
}
interface MyInterface {
data: Record<string, Item>;
}
const obj: MyInterface = {
data: {
"123": { id: "123", name: "something" }
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9090 次 |
| 最近记录: |