DefinitelyTyped/types/angular
Nathan Shively-Sanders c8089e91d1
Trim tsconfig files; move untested files to OTHER_FILES.txt (#40676)
* Initial cut

* fix aos global reference

* fix clearbladejs global references

* fix cldr.js augmentations

* fix codemirror compile errors (maybe)

* fixup skipped files after merge

* fix dwt/v13

* add missing references to adone

* fix meteor (manually)

* fix rangy

* add missing file reference to react-blessed

* fix react-dom?

* bump codemirror to 3.2

* bump dwt to 3.2

* Add/remove OTHER_FILES.txt as needed.

* bump react-codemirror to 3.2

* add references to slickgrid tests

* add reference to strophe.js tests

* add reference to strophe+fix types

* add reference to waypoints test

* Bump others to 3.9

* remove incorrectly added file

* Use more explicit types reference paths

* bump strophejs-plugin-roster TS version
2019-11-26 12:47:30 -08:00
..
test
angular-component-router.d.ts
angular-tests.ts accept Window as argument to JQueryStatic (#37567) 2019-08-12 14:36:54 -07:00
index.d.ts
jqlite.d.ts
OTHER_FILES.txt
README.md
tsconfig.json Trim tsconfig files; move untested files to OTHER_FILES.txt (#40676) 2019-11-26 12:47:30 -08:00
tslint.json

AngularJS Definitions Usage Notes

Referencing AngularJS definition files in your code

Read the TypeScript handbook for details on consuming these type definitions.

If you are including other AngularJS' modules in your code, like ngResource, just like you needed to include the additional module implementation file in your code, angular-resource.js, you will also need to reference the definitions file related to that module. Your code would then have the following definitions files reference:

Having these modules in separated packages is actually good because they sometimes either augment or modify some of ng's interfaces, and thus those differences should only be available to you when you really need them. Also, it forces you to explicit what you're going to be using.

The following extra definition files are available for referencing:

  • angular-resource/index.d.ts (for the ngResource module)
  • angular-route/index.d.ts (for the ngRoute module)
  • angular-cookies/index.d.ts (for the ngCookies module)
  • angular-sanitize/index.d.ts (for the ngSanitize module)
  • angular-animate/index.d.ts (for the ngAnimate module)
  • angular-mocks/index.d.ts (for the ngMock and ngMockE2E modules)

The Angular Static

The definitions declare the AngularJS static variable angular as ambient. That means that, after referencing the AngularJS definition, you will be able to get type checks and code assistance for the global angular member.

Definitions modularized

To avoid cluttering the list of suggestions as you type in your IDE, all interfaces reside in their respective module namespace after you include their respective definitions:

  • ng for AngularJS' ng module
  • ng.auto for AUTO
  • ng.cookies for ngCookies
  • ng.mock for ngMock
  • ng.resource for ngResource
  • ng.route for ngRoute
  • ng.sanitize for ngSanitize
  • ng.animate for ngAnimate

ngMockE2E does not define a new namespace, but rather modifies some of ng's interfaces.

Below is an example of how to use the interfaces:

function MainController($scope: ng.IScope, $http: ng.IHttpService) {
    // code assistance will now be available for $scope and $http
}

Services and other injectables

AngularJS makes vast use of what it calls "injectable" functions. To put it simple, in AngularJS you are constantly annotating your functions and constructors with their dependencies, services that are going to be provided as arguments automagically during invocation.

All known services interfaces have been defined, and were named using the following convention:

I + 'ServiceName' + 'Service'

So, for instance, the $parse service has it's interface defined as ng.IParseService.

Service providers, by the same logic, follow this convention:

I + 'ServiceName' + 'Provider'

The $httpProvider, thus, is defined by ng.IHttpProvider.

A word on $scope and assigning new members

TypeScript allows for static checking. Among other obvious things, that means you're gonna have to extend interfaces when you need to augment an object whose interface has been defined, because otherwise the compiler will see it as an error to try to assign a value to a unspecified member.

Consider the following ordinary code:

function Controller($scope) {
    $scope.$broadcast('myEvent');
    $scope.title = 'Yabadabadu';
}

That will not produce any compilation error because the compiler does not know the first thing about $scope to do any checking. For that same reason, you will not get any assistance either.

Now consider this:

function Controller($scope: ng.IScope) {
    $scope.$broadcast('myEvent');
    $scope.title = 'Yabadabadu';
}

Now we annotated $scope with the interface ng.IScope. The compiler now knows that, among other members, $scope has a method called $broadcast. That interface, however, does not define a title property. The compiler will complain about it.

Since you are augmenting the $scope object, you should let the compiler know what to expect then:

interface ICustomScope extends ng.IScope {
    title: string;
}

function Controller($scope: ICustomScope) {
    $scope.$broadcast('myEvent');
    $scope.title = 'Yabadabadu';
}

Examples