简体   繁体   English

出口进口<name>打字稿中的语法?</name>

[英]export import <name> syntax in TypeScript?

While browsing a declaration file on dt I came accross the following syntax:dt上浏览声明文件时,我遇到了以下语法:

import github = Strategy; // <- what is imported from where here?

declare class Strategy extends oauth2.Strategy {
     ...
}

declare namespace Strategy {
    export import Strategy = github; // <- I've not seen this syntax before?

    interface _StrategyOptionsBase {
      ...
    }
}

export = Strategy;

I'm having trouble understanding the import and export syntax here.我无法理解此处的导入和导出语法。 I roughly understand typescripts export= custom syntax .我大致了解了 typescripts export= custom syntax However, I don't understand why and what we're exporting from the namespace.但是,我不明白我们从命名空间导出的原因和内容。 Nor do I fully understand what we're importing with import github... .我也不完全理解我们正在使用import github...什么。

I'd love some help.我需要一些帮助。

import github = Strategy;

this syntax describes a namespace alias.此语法描述名称空间别名。

Another way that you can simplify working with namespaces is to use import q = xyz to create shorter names for commonly-used objects.另一种可以简化使用名称空间的方法是使用import q = xyz为常用对象创建更短的名称。

 namespace Shapes { export namespace Polygons { export class Triangle {} export class Square {} } } import polygons = Shapes.Polygons; let sq = new polygons.Square(); // Same as 'new Shapes.Polygons.Square()'

source 资源


export import Strategy = github

is a combination of renaming the namespace and exporting it again with the new name.是重命名命名空间和使用新名称再次导出它的组合。 You can't write something like你不能写类似的东西

declare namespace Strategy {
    export namespace Strategy;
}

so this looks like a workaround to achieve what is done in the passport-github source code.所以这看起来像是一种解决方法,可以实现在passport-github源代码中完成的工作。

// Load modules.
var Strategy = require('./strategy');

// Expose Strategy.
exports = module.exports = Strategy;

// Exports.
exports.Strategy = Strategy;

source 资源

exports = module.exports = Strategy;

this line exports Strategy as default export, the counterpart in the typescript definition is此行将Strategy导出为默认导出,打字稿定义中的对应项是

export = Strategy

Then passport-gitlab also export Strategy as property on the default export.然后 passport-gitlab 也将Strategy导出为默认导出的属性。

exports.Strategy = Strategy;

You can think of it like a class that has a static property which references the class.您可以将其视为具有引用该类的静态属性的类。

class Strategy {
    static Strategy: typeof Strategy
}
export = Strategy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM