Typescript : Alias a long module name
Yes there is a way. import
can be used to import a module or also to define a type name to make it shorter. Here's an example of the latter:
declare module alpha.bravo.charlie {
export class Delta {
constructor();
}
}
import Delta = alpha.bravo.charlie.Delta;
let d: Delta = new Delta();
You can also save typing by letting the type inference system do some of the work.
let d = new Delta();
More info: https://www.typescriptlang.org/docs/handbook/namespaces.html#aliases
From 1.4 we have ability to use type
keyword.
With TypeScript 1.3 out the door, we're focused on adding more type system and ECMAScript 6 features to TypeScript. Let's take a quick look at some of the new features you'll be able to use in the next release of TypeScript. All these features are live in the master branch on our GitHub repository if you'd like to check them out yourself today.
With these features, we can more accurately and easily work with variables and expressions that may have different types at runtime. Together, these features help reduce the need for explicit type annotations, type assertions, and use of the 'any' type. Type definition file (.d.ts) authors can use these to more precisely describe external libraries. For those following the development of the compiler, you'll notice we're already using these features in the compiler today.
For more information, please visit: TypeScript 1.4 sneak peek: union types, type guards, and more
Example:
Let's to create simple model: basemodel.ts
module NameSpace {
export module Model {
export class BaseModel {
public Id: number;
public Name: string;
}
}
}
and we need to use this model in our controller.
/// <reference path="path to base model.ts" />
type BaseModel = NameSpace.Model.BaseModel;
module NameSpace {
export module Controller {
export class BaseController {
public entities: new Array<BaseModel>();
constructor(externalEntities: Array<BaseModel>) {
this.entities = externalEntities;
}
}
}
}