31

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,

4
  • Not sure about typescript specific. But in Javascript you need to load the main angular source code. Commented Apr 28, 2015 at 9:16
  • Yes, after the tsc command, I'll get a .js file to be included into html page as usual. Commented Apr 28, 2015 at 9:18
  • I see, maybe you need to tell typescript to treat angular as a global dependency.. Commented Apr 28, 2015 at 9:22
  • 1
    Have a look at this project in typescript and angular - github.com/tastejs/todomvc/blob/gh-pages/examples/… Commented Apr 28, 2015 at 9:23

7 Answers 7

39

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.

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

6 Comments

Just to add for the non-visual studio folks, take a look at this tool to assist on getting individual type definitions without pulling down the entire repo... github.com/DefinitelyTyped/tsd (can work like bower/npm via config file)
I installed the nuget package today (DefinitelyTyped). It's broken.
bad solution, this removes any typescript type support from the imported *.d.ts file for angular
@sohnee that is a great assumption. Perhaps the answer could be rewritten so you can't make that assumption.
@adam0101 I have replaced the broken like with a link to the type search utility. This will find any available definition and link through to the NPM page for it.
|
24

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

Comments

7

You need to include angular.t.ds file in the library

1 Comment

What you mean is angular.d.ts . Isn't it? I think there is a typo in the file name in your answer.
3

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.

Comments

1

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.

Comments

0

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.

Comments

0

Make sure that in the tsconfig.json the filesGlob points and exposed the typings file:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.