Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript : Alias a long module name

Tags:

typescript

I looked this up already, going to the following questions:

Alias External Module in TypeScript

Type reference without full namespace

But I am still confused. This is not a "requirement", just a desire. I am using Kendo UI and their MVVM architecture in Typescript, and I find myself constantly typing this out ...

class Item extends ItemPrototype {
   public Quality: kendo.data.ObservableObject = new kendo.data.ObservableObject();
}

Yeah, that works fine, but it is getting obnoxious. Is there a way I can set up typescript to let me use a short hand? Like ...

class Item extends ItemPrototype {
   public Quality: observable = new observable();
}
like image 975
Ciel Avatar asked Jan 21 '14 20:01

Ciel


People also ask

How do I create alias in TypeScript?

In Typescript, Type aliases give a type a new name. They are similar to interfaces in that they can be used to name primitives and any other kinds that you'd have to define by hand otherwise. Aliasing doesn't truly create a new type; instead, it gives that type a new name.

What is the difference between namespace and module in TypeScript?

Namespaces are a TypeScript-specific way to organize code. Namespaces are simply named JavaScript objects in the global namespace. This makes namespaces a very simple construct to use. Unlike modules, they can span multiple files, and can be concatenated using outFile .

Should I use TypeScript namespace?

Don't use Custom TypeScript Modules and Namespaces Since we have ES6 modules as a standard in JavaScript, we don't need custom TypeScript modules and namespaces to organize our code. Instead, we should use standard JavaScript modules with import and export instead.

How do you define a namespace in TypeScript?

Namespace Declaration We can create a namespace by using the namespace keyword followed by the namespace_name. All the interfaces, classes, functions, and variables can be defined in the curly braces{} by using the export keyword. The export keyword makes each component accessible to outside the namespaces.


2 Answers

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

like image 64
Jeffery Grajkowski Avatar answered Oct 21 '22 04:10

Jeffery Grajkowski


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;
            }
        }
    }
}
like image 45
d.danailov Avatar answered Oct 21 '22 03:10

d.danailov