如何在 TypeScript 中通过导入使用命名空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37565709/
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-09-09 07:09:52  来源:igfitidea点击:

How to use namespaces with import in TypeScript

typescriptnamespaceses6-modules

提问by gevik

I have two classes in two separate files and one extends from another. The base class contains some importstatements using node modules. It is unclear to me why the derived class (which is in a separate file) does not recognize the base class!!!???

我在两个单独的文件中有两个类,一个从另一个扩展。基类包含一些import使用节点模块的语句。我不清楚为什么派生类(在一个单独的文件中)不能识别基类!!!???

Can someone clarify this please?

有人可以澄清一下吗?

// UtilBase.ts

/// <reference path="../typings/node.d.ts" />
/// <reference path="../typings/packages.d.ts" />

import * as path from "path"; // <---- THIS LINE BREAKS THE BUILD!!!!

namespace My.utils {

    export class UtilBase {

        protected fixPath(value: string): string {
            return value.replace('/', path.sep);
        }
   }
}

And then

进而

// UtilOne.ts
/// <reference path="UtilBase.ts" />

namespace My.utils {

    export class UtilOne extends My.utils.UtilBase {

    }
}

After compiling I get:

编译后我得到:

src/UtilOne.ts(6,47): error TS2339: Property 'UtilBase' does not 
exist on type 'typeof utils'

采纳答案by Paleo

A solution with namespaces(not recommended)

带有命名空间的解决方案(不推荐)

To resolve your issue, you can export your namespace:

要解决您的问题,您可以导出您的命名空间:

// UtilBase.ts
import * as path from "path";
export namespace My.utils {
    export class UtilBase {
        protected fixPath(value: string): string {
            return value.replace('/', path.sep);
        }
   }
}

Then, you should be able to import it:

然后,您应该能够导入它:

// UtilOne.ts
import {My} from './UtilBase';
namespace My.utils {
    export class UtilOne extends My.utils.UtilBase {
    }
}

However, if the purpose is to organize the code, it is a bad practice to use namespacesand (ES6) modulesat the same time. With Node.js, your files are modules, then you should avoid namespaces.

但是,如果目的是组织代码,同时使用命名空间(ES6)模块是一种不好的做法。使用 Node.js,你的文件是模块,那么你应该避免命名空间。

Use ES6 moduleswithout namespaces

使用没有命名空间的ES6 模块

TypeScript supports the syntax of ES6 modules very well:

TypeScript 很好地支持 ES6 模块的语法:

// UtilBase.ts
import * as path from "path";
export default class UtilBase {
    protected fixPath(value: string): string {
        return value.replace('/', path.sep);
    }
}

// UtilOne.ts
import UtilBase from './UtilBase';
export default class UtilOne extends UtilBase {
}

It is the recommended way. ES6 modules prevent naming conflicts with the ability to rename each imported resource.

这是推荐的方式。ES6 模块可以通过重命名每个导入的资源来防止命名冲突。

It will work on Node.js (using the commonjsmodule syntax in compiler options).

它将在 Node.js 上运行(使用commonjs编译器选项中的模块语法)。

For a good introduction to the ES6 modules syntax, read this article.

有关 ES6 模块语法的良好介绍,请阅读这篇文章

Use a file tsconfig.jsoninstead of /// <reference

使用文件tsconfig.json代替/// <reference

Notice: The syntax /// <referenceis replaced by the file tsconfig.json. An example for Node.js:

注意:语法/// <reference替换为文件tsconfig.json. Node.js 的一个例子:

// tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6"
  },
  "exclude": [
    "node_modules"
  ]
}