Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TS-2304 Error - can not find name 'Iterable' in TypeScript while importing "jquery" in ".ts" file

I am working on TypeScript version 2.4 using Visual Studio Code as an editor. I installed jQuery with NPM using the following command:

npm install --save @types/jquery

Then I downloaded the source code of jquery module from GitHub.

The code of registrationpage.ts file is as follows:

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();
    }
}

The code for index.html is as follows:

<!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 file :

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

    }

}

When I compile the code on CMD , I get the following error:

ERROR TS2304 : cannot find the name Iterable

Please suggest an appropriate solution.

like image 561
Swinky Avatar asked Jul 28 '17 11:07

Swinky


2 Answers

The configuration of tsconfig.json is correct. When the target is set to ES5 and libraries : es2015,es2015-iterable are included then Iterable is supported. The point is when we compile the .ts file by command tsc "filename" then "tsconfig.json" is ignored by the compiler. Also, while working with external modules, you need to include the following js files to support module loading:

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

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

Lastly, Compile the file with this command

**tsc -p .**

This command will not ignore tsconfig.json file.

Hope it helps.

like image 52
Swinky Avatar answered Sep 30 '22 05:09

Swinky


Consider installing the node type definitions.

npm i --save-dev @types/node

If you are using Yarn instead of NPM:

yarn add -D @types/node

like image 43
Igor P Avatar answered Sep 30 '22 06:09

Igor P