Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting Error:(45, 1) TS2304: Cannot find name 'angular'. in WebStorm [duplicate]

I am just starting out with TypeScript and trying to add it to an existing AngularJS project.

I have 'excluded' the bower directory where angular is installed and downloaded the definitelyTyped definitions for angular in the preferences window.

Angular code completion is working, but typescript is giving me an error TS2304 wherever I code 'angular.'.

What have I missed?

like image 777
kpg Avatar asked Mar 25 '15 15:03

kpg


3 Answers

To fix the error, you need to copy the downloaded definitelyTyped TypeScript stubs from ~/Library/Caches/WebStorm9/extLibs folder to your project directory and reference them in your .ts file using /// <reference path> comments, like

/// <reference path="path/to/angular.d.ts" />

Just to make things clear: When you download Typescript stubs via Preferences/Languages & Frameworks/Javascript/libraries, they are placed into ~/Library/Caches/WebStorm9/extLibs. It's perfectly fine for Webstorm - it doesn't require placing library files directly in the project folder. Also, Webstorm itself doesn't need ///reference comments to be able to resolve types - type hinting/navigation/completion works even if the types are not explicitly referenced. But the tsc compiler does need the d.ts files being placed somewhere in the project directory and referenced via ///reference comment. So, to get downloaded stubs available to typescript compiler, you need to copy/move them to you project directory (and probably rename to more human readable names :)) and add comments (can be done using 'Generate reference path comment' intention (hit Alt+Enter on a reference to generate a comment)). We plan to provide an option to download files directly to the project folder (instead of system/extLibs/ ) in the future versions

like image 141
lena Avatar answered Oct 20 '22 05:10

lena


probably need to add a reference to the .d.ts in the particular .ts file having the problem, something like:

/// <reference path="../typings/angular.d.ts"/>
like image 21
James Manning Avatar answered Oct 20 '22 07:10

James Manning


downloaded the definitelyTyped definitions for angular in the preferences window.

The intent of downloading the stubs like that is to provide intellisense for JavaScript code not TypeScript code. For TypeScript code you should download and reference the .d.ts directly in your TypeScript code.

like image 1
basarat Avatar answered Oct 20 '22 05:10

basarat