Jay
Jay

Reputation: 3531

Typescript namespace inside module - how to reference it?

The type definition for the ngstorage library is as follows:

import * as angular from 'angular';

declare module 'angular' {
  export namespace storage {

    export interface IStorageService {
      //interface definition
    }

    export interface IStorageProvider extends angular.IServiceProvider {
      //interface definition
    }
  }
}

Three questions:

  1. What exactly happens when the already-existing module 'angular' is declared again, as happens on line 3 of this example?
  2. What does it mean to export a namespace inside a module definition export namespace storage?
  3. How do I import IStorageService into my .ts files? Nothing I've tried so far works.

    import { IStorageService } from 'ngstorage'

    import { IStorageService } from 'angular.storage'

    or just directly refer to IStorageService like so:

    angular.storage.IStorageService

Upvotes: 0

Views: 649

Answers (1)

Nitzan Tomer
Nitzan Tomer

Reputation: 164129

It's called Module Augmentation, and it's a way to add definitions to existing modules.
In this case, when you are importing the file that you posted it will add the new definitions to the angular module, just like the js file does at runtime.

You should be able to import it like so:

import * as angular from 'ngstorage';

let a: angular.storage.IStorageService;
...

Edit

Try:

import * as angular from 'angular';
import 'ngstorage';

let a: angular.storage.IStorageService;

Upvotes: 1

Related Questions