2017-07-28 77 views
4

我正在使用Visual Studio Code作为编辑器的TypeScript版本2.4。我安装jQuery的与NPM使用下面的命令:TS-2304错误 - 在“.ts”文件中导入“jquery”时无法在TypeScript中找到名称'Iterable'

npm install --save @types/jquery 

然后我下载的jquery module源代码从GitHub

registrationpage.ts文件的代码如下:

import * as $ from 'jquery'; 
class Registration_Page { 
    txtuname: string; 
    Registration() { 
     $('#table').hide; 
     this.txtuname = (<HTMLTextAreaElement> (document.getElementById('txtusername'))).value; 
    } 
} 
window.onload =() => { 
    $('#table').show; 
    var bttnLogin = document.getElementById('submit'); 
    var obj = new Registration_Page(); 
    bttnLogin.onclick = function() { 
     obj.Registration(); 
    } 
} 

index.html的代码如下:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
    <script src="registrationpage.js"></script> 

</head> 
<body> 
    <form id="form1"> 
    <div> 
    <fieldset style="font-size: medium; color: #000000;"> 
     <legend style="background-color:#CCCCFF; font-size: larger;font-weight:bold">Registration Page in TypeScript</legend> 
     <br /> 
     <b>UserName</b> 
     <input id="txtusername" type="text" /><br /> 
     <br /> 

     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      <input id="submit" type="button" value="Submit" />&nbsp;&nbsp;&nbsp;&nbsp; 

    </fieldset> 
    </div> 
    <div id="table"> 
     <table> 
      <tr> 
       <th>UserName</th> 

      </tr> 
     </table> 
    </div> 
    </form> 
</body> 
</html> 

tsconfig.json文件:

{ 
    "compilerOptions": { 
     "target": "es5", 
     "noImplicitAny": true, 
     "lib": [ "es2015", "dom" ] 

    } 

} 

当我编译代码在CMD上,我得到以下错误:

ERROR TS2304 : cannot find the name Iterable

请建议适当的解决方案。

+0

你找到一个解决的办法,我有同样的问题 –

+0

我在得到这个同样的问题VS 2017 UI。从你的答案下面,它听起来像tsconfig.json被忽略?你能提供更多细节吗?如何解决这个问题? –

+0

是的,它忽略了tsconfig.json文件。解决方案是使用命令编译代码:“tsc -p”。 请参考:“https://www.typescriptlang.org/docs/handbook/tsconfig-json.html” – Swinky

回答

2

tsconfig.json的配置是正确的。当目标设置为ES5并且包含库:es2015,es2015-iterable时,则支持Iterable。 关键是当我们通过命令tsc "filename"编译.ts文件时,编译器会忽略“tsconfig.json”。此外,在与外部模块的工作,你需要包含以下js文件来支持模块加载:

https://unpkg.com/core-js/client/shim.min.js"> 

https://unpkg.com/[email protected]/dist/system.src.js 

最后,编译该文件使用此命令

**tsc -p .** 

这个命令不会忽略tsconfig.json文件。

希望它有帮助。

+0

这应该是被接受的答案。没有人提到'tsc'命令默认情况下忽略了'tsconfig.json'。谢谢! – aberkow

1

考虑安装节点类型定义。

npm i --save-dev @types/node

如果您正在使用,而不是NPM

yarn add -D @types/node

+0

节点类型文件定义了'Iterable'用于在第61行附近使用'--lib es5'。请参阅* node_modules/@ types/node/index.d.ts * – preist

相关问题