typescript 打字稿:别名很长的模块名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21268170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:53:12  来源:igfitidea点击:

Typescript : Alias a long module name

typescript

提问by Ciel

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

我已经查过这个,去回答以下问题:

Alias External Module in TypeScript

TypeScript 中的别名外部模块

Type reference without full namespace

没有完整命名空间的类型引用

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

但我仍然很困惑。这不是“要求”,只是一种愿望。我Kendo UI在 Typescript 中使用他们的 MVVM 架构,我发现自己不断地输入这个......

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();
}

回答by Jeffery Grajkowski

Yes there is a way. importcan be used to import a module or also to define a type name to make it shorter. Here's an example of the latter:

是的,有办法。 import可用于导入模块或定义类型名称以使其更短。下面是后者的一个例子:

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();

回答by d.danailov

From 1.4 we have ability to use typekeyword.

从 1.4 开始,我们可以使用type关键字。

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.

随着 TypeScript 1.3 的发布,我们专注于向 TypeScript 添加更多类型系统和 ECMAScript 6 功能。让我们快速浏览一下您将能够在下一版 TypeScript 中使用的一些新功能。如果您今天想亲自检查一下,所有这些功能都在我们 GitHub 存储库的 master 分支中。

借助这些功能,我们可以更准确、更轻松地处理运行时可能具有不同类型的变量和表达式。总之,这些功能有助于减少对显式类型注释、类型断言和“any”类型的使用的需要。类型定义文件 (.d.ts) 作者可以使用这些来更准确地描述外部库。对于那些关注编译器开发的人,您会注意到我们今天已经在编译器中使用了这些功能。

For more information, please visit: TypeScript 1.4 sneak peek: union types, type guards, and more

更多信息,请访问:TypeScript 1.4 先睹为快:联合类型、类型保护等

Example:

例子:

Let's to create simple model: basemodel.ts

让我们创建简单的模型: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;
            }
        }
    }
}