我在打字时很难打字 Map
对象 typescript 1.8.10
。这是摘录自 core-js
定义Map接口:
interface Map<K, V> {
clear(): void;
delete(key: K): boolean;
forEach(callbackfn: (value: V, index: K, map: Map<K, V>) => void, thisArg?: any): void;
get(key: K): V;
has(key: K): boolean;
set(key: K, value?: V): Map<K, V>;
size: number;
}
我想创建一个使用字符串键的地图,并且只存储具有该形状的值 {name:string,price:number}
。我尝试用以下方式声明我的对象:
let oMap:Map<string,{name:string,price:number}> = new Map();
但是,编译器会抛出错误 TS2322: Type 'Map<{}, {}>' is not assignable to type 'Map<string, { name: string; price: number; }>'
。使用ES6时有没有办法利用强类型 Map
打字稿中的对象?