简体   繁体   中英

Getting Error - Cannot find name 'angular'

I'm starting writing AngularJS app using TypeScript. I have this simple module command:

(() => {
    angular
        .module('app', []);
})();

When I run tsc app.ts , I got this: app.ts(2,5): error TS2304: Cannot find name 'angular'.

Do I need to include something to app.ts to make tsc understand the angular ?

Regards,

You could simplistically tell the compiler to stop worrying by telling it that "you know all about angular":

declare var angular: any;

To get all the good tooling and type checking, you need to pull in the Angular type definition so the compiler knows what is available.

The type definitions are available via NuGet if you are using Visual Studio or various other methods for your favourite IDE.

Using AngularJS 1.x and TypeScript 2.x, I solved this issue by running:

npm install --save-dev @types/angular

and then include the following line on top of the .ts file:

import * as angular from "angular";

Reference: The Future of Declaration Files

您需要在库中包含angular.t.ds文件

For me inside of VisualStudio I used part of Augusto's suggested answer and simply did:

npm install --save @types/angular

and it worked like a charm.

ATOM

In tsconfig.json file ensure your "fileGlobs" points to both:-

  1. The TypeScript Definitions you installed with tsd &
  2. Your src .ts files
"filesGlob": [
  "./typings/**/*.ts",
  "./src/client/**/*.ts"
]

This will ensure you have tooling for the libraries you have installed (with tsd ) without having to declare any variables. For example, if you tsd install angularjs --save , you can work angular in your src/../*.ts files with all the goody tooling.

Hope this helps. Good Luck.

I solved the problem by adding the following line to my package.json, inside the "devDependencies" section:

"@types/angular" : "1.6.17"

after that, i just needed to run

npm install

in order for npm to download all the needed dependencies.

确保在tsconfig.jsonfilesGlob指向并公开了filesGlob文件:

"filesGlob": ["typings.d.ts" ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM