打字稿错误:Map.values()赋予IterableIterator不可迭代 [英] Typescript error: Map.values() giving IterableIterator not Iterable

查看:37
本文介绍了打字稿错误:Map.values()赋予IterableIterator不可迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试遍历 Map.values()(其中Map的类型为< number,Foo>)的返回值时,Typescript给了我这个错误:

错误TS2495:键入"IterableIterator< Foo>"不是数组类型或字符串类型.

根据ES6 doc Map.values()应该返回一个 Iterable 而不是IterableIterator,我应该能够在for-of循环中使用它.

这在 node 中工作正常:

  var数据= [{id:0},{id:1},{id:3}];var m = new Map(data.map(n => [n.id,n]));for(m.values()的var i){console.log(i)} 

这给出了来自 tsc 的错误:

 界面Foo {身份证号码;}var数据:Foo [] = [{id:0},{id:1},{id:2}];var m = new Map (data.map(n =< [number,Foo]> [n.id,n])));for(m.values()的var i){console.log(i)} 

我从 @ types/core-js @ 0.9.34 获取Map声明,所以我想问题出在此声明中?

其他版本&conf信息:

 >tsc --version版本2.0.3>猫tsconfig.json{"compilerOptions":{"target":"es6","module":"commonjs","moduleResolution":节点","sourceMap":是的,"emitDecoratorMetadata":是的,"experimentalDecorators":是的,"removeComments":否,"noImplicitAny":是的,"suppressImplicitAnyIndexErrors":是的,"typeRoots":["./node_modules/@types/"]},"compileOnSave":是的,排除": ["node_modules/*","**/*-aot.ts"]} 

解决方案

我不了解 tsc 的某些隐式内容与 tsconfig.json 的关系.阅读 tsconfig.json文档可以使事情变得更加清晰:

首先,我像这样 tsc mytypescript.ts 进行编译,这(愚蠢地)导致Typescript默默地忽略 tsconfig.json 文件,这意味着它使用的是默认的ES5.但这部分起作用,因为 tsc 仍在找到 core-js 声明,其中包含ES6声明,例如Map,Iterable等.这使我的调试工作有些麻烦.

第二,在让Typescript实际获取我的配置后,无论如何该配置是错误的.我实际上不需要或不需要 @ types/core-js 中的那些声明(很确定).是的,我将在项目中使用core-js作为polyfill,但是Typescript 附带了自己的ES6声明 typescript/lib/lib.es6.d.ts 中的内容,而 @ types/core-js 中的内容又旧又怪异或cr脚……

Typescript is giving me this error when I try to iterate over the value returned from Map.values() (where the Map has type <number,Foo>):

error TS2495: Type 'IterableIterator<Foo>' is not an array type or a string type.

According to the ES6 doc Map.values() should return an Iterable not an IterableIterator and I should be able to use it in a for-of loop.

This works fine in node:

var data = [
  {id: 0},
  {id: 1},
  {id: 3}
];
var m = new Map(data.map(n => [n.id,n]));
for(var i of m.values()) { console.log(i) }

This gives the error from tsc:

interface Foo {
  id: number;
}
var data: Foo[] = [
  {id: 0},
  {id: 1},
  {id: 2}
];
var m = new Map<number,Foo>(data.map(n => <[number,Foo]>[n.id,n]));
for(var i of m.values()) { console.log(i) }

I'm getting Map declarations from @types/core-js@0.9.34 so I guess the issue is in this declaration??

Other version & conf info:

> tsc --version
Version 2.0.3
> cat tsconfig.json 
{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true,
    "typeRoots": [
      "./node_modules/@types/"
    ]
  },
  "compileOnSave": true,
  "exclude": [
    "node_modules/*",
    "**/*-aot.ts"
  ]
}

解决方案

I wasn't understanding some of the implicit stuff tsc is doing rel the tsconfig.json. Reading tsconfig.json doc made things clearer:

First, I was transpiling like this tsc mytypescript.ts which (stupidly) causes Typescript to silently ignore tsconfig.json file, which meant it was using the default ES5. But this was partially working because tsc was still finding the core-js declarations which contain declaration for ES6 things like Map, Iterable etc. That threw my debugging a bit.

Second, after getting Typescript to actually pick up my config, the config was wrong anyway. I don't actually need or want those declarations from @types/core-js (pretty sure). Yes, I'll use core-js as a polyfill in my project, but Typescript comes with it's own declarations for ES6 stuff in typescript/lib/lib.es6.d.ts, and the ones in @types/core-js are old weird and crappy or something ...

这篇关于打字稿错误:Map.values()赋予IterableIterator不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆