我在强打字时遇到困难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 https://en.wikipedia.org/wiki/ECMAScript#6th_Edition_%E2%80%93_ECMAScript_2015 Map
TypeScript 中的对象?