TypeScript编译器在使用新Map()时遇到困难

时间:2017-01-16 11:35:43

标签: typescript typescript2.0

我正在尝试使用TypeScript Map数据结构,但是我得到了以下编译器错误:

argument of type '(string | number)'[][] is not assignable to parameter of type 'iterable<[{},{}]>

我的地图:

 export const PostTypeMap = new Map([
    [0, 'Link'],
    [1, 'Status'],
    [2, 'Photo'],
    [3, 'Video'],
    [4, 'Offer'],
    [5, 'Unknown'],
    [6, 'Event']
]);

我的tsconfig.js

{
  "compilerOptions": {
    "target": "ES5",
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "suppressImplicitAnyIndexErrors": true,
    "allowSyntheticDefaultImports":true
  },
  "awesomeTypescriptLoaderOptions": {
    "resolveGlobs": true,
    "forkChecker": true
  },
  "compileOnSave": false,
  "buildOnSave": false
}

1 个答案:

答案 0 :(得分:0)

有必要表示嵌套数组中只有两个项,而不仅仅是任意数量的项:

export const PostTypeMap = <[number, string][]> new Map([
    [0, 'Link'],
    [1, 'Status'],
    [2, 'Photo'],
    [3, 'Video'],
    [4, 'Offer'],
    [5, 'Unknown'],
    [6, 'Event']
]);