8

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?

0

3 Answers 3

12

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

Sign up to request clarification or add additional context in comments.

4 Comments

Rather than copy the files from WebStorm's system lib I installed a separate copy of the type definitions using tsd. It would certainly save some work if WebStorm did that directly.
is that true for Angular2 too?
more or less. What is your issue - 'TS2307: Cannot find module 'angular2/core''? Compiler can't find the d.ts files imported by your module. If you don't have /// <reference path comments pointing to d.ts files, and don't like adding them, using tsconfig.json is your choice: if referenced files are included in it, and tsconfig is used for compilation ('Use tsconfig.json' is enabled in Settings/Languages & Frameworks/TypeScript), compiler will use the configuration file when searching for references, and won't need d.ts files being referenced explicitly.
2

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"/>

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.