2017-07-28 305 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

相關問題