我正在使用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 />
<input id="submit" type="button" value="Submit" />
</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
请建议适当的解决方案。
你找到一个解决的办法,我有同样的问题 –
我在得到这个同样的问题VS 2017 UI。从你的答案下面,它听起来像tsconfig.json被忽略?你能提供更多细节吗?如何解决这个问题? –
是的,它忽略了tsconfig.json文件。解决方案是使用命令编译代码:“tsc -p”。 请参考:“https://www.typescriptlang.org/docs/handbook/tsconfig-json.html” – Swinky