Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Import a namespaced TypeScript class in Angular 2?

Given a TypeScript class which is namespaced as a TS module, in a file CoolApps.Utilities.ts:

    module CoolApps {
        export class Utilities {
            myMethod(){
               alert("something awesome");
            }
        }
    }

The class works in a normal TypeScript app but I'm trying to figure our the correct way to reference this class in Angular 2. How do I use this in an Angular 2 app (Ionic 2 in the case)? So far the following doesn't resolve so I'm probably getting the syntax wrong:

import {Page} from 'ionic-framework/ionic';
import {Utilities} from '../../core/CoolApps.Utilities';

Using a reference like so will allow the editor to see the code as valid, but Angular can't resolve it (maybe import only works for Angular specific modules?):

///<reference path="../../core/mapping/OCM.Mapping.ts"/>
like image 893
Christopher Cook Avatar asked Dec 21 '15 05:12

Christopher Cook


People also ask

How do I import a namespace in TypeScript?

Use a file tsconfig. @Pavel_K In the TypeScript handbook: "To reiterate why you shouldn't try to namespace your module contents, the general idea of namespacing is to provide logical grouping of constructs and to prevent name collisions.


1 Answers

Import :

import {CoolApps} from '../../core/CoolApps.Utilities';

Class usage example

let util : CoolApps.Utilities  = new CoolApps.Utilities();

You can also remove module declaration from CoolApps.Utilities.ts and transform the import like this :

import * as CoolApps from '../../core/CoolApps.Utilities';
like image 106
Mourad Zouabi Avatar answered Oct 23 '22 05:10

Mourad Zouabi