Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Angularjs with TypeScript

I want to use Angularjs with TypeScript.

To solve TS2095: Could not find symbol 'angular' error, I downloaded ts files from here and added references to controllers.ts like this:

/// <reference path="angular.d.ts" />
/// <reference path="jquery.d.ts" />
var mathML = angular.module('mathML', []);

But I still have similar error like this:

angular.d.ts(33,28): error TS2095: Could not find symbol 'JQuery'.

How can I solve this error? Is there better way to setup environment to use Angularjs with TypeScript?

like image 935
ironsand Avatar asked Jun 14 '14 04:06

ironsand


People also ask

Can I use TypeScript with AngularJS?

Guide to using TypeScript in AngularJS applications. Supporting older AngularJS applications doesn't mean you can't take advantage of modern tools like TypeScript. Not every file in your application needs to be written in TypeScript at once, you just can rename all your JavaScript files to have a . ts extension.

Does AngularJS use TypeScript or JavaScript?

Angular is a modern framework built entirely in TypeScript, and as a result, using TypeScript with Angular provides a seamless experience. The Angular documentation not only supports TypeScript as a first-class citizen, but uses it as its primary language.

How does TypeScript relate to Angular?

TypeScript is a primary language for Angular application development. It is a superset of JavaScript with design-time support for type safety and tooling. Browsers can't execute TypeScript directly. Typescript must be "transpiled" into JavaScript using the tsc compiler, which requires some configuration.

Do you have to use TypeScript with Angular?

You can write Angular applications in either TypeScript, ES6 or even ES5 JavaScript. However Angular itself is written in TypeScript, most examples on the web are written in TypeScript, most Angular jobs require you to write TypeScript so this book will be teaching in TypeScript.


1 Answers

Using the following directory structure :

|- angular
    |- angular.d.ts
|- jquery
    |- jquery.d.ts
|- mathML.ts

I was able to get the following sample code to compile without errors.

/// <reference path="angular/angular.d.ts" />
var mathML = angular.module('mathML', []);

The angular.d.ts file references the jquery.d.ts file using a relative path. The directory structure is pretty standard for typescript code.

like image 179
Alex Avatar answered Sep 19 '22 21:09

Alex