简体   繁体   English

如何在TypeScript中使用导入的名称空间

[英]How to use namespaces with import in TypeScript

I have two classes in two separate files and one extends from another. 我在两个单独的文件中有两个类,一个从另一个扩展。 The base class contains some import statements using node modules. 基类包含一些使用节点模块的import语句。 It is unclear to me why the derived class (which is in a separate file) does not recognize the base class!!!??? 我不清楚为什么派生类(在一个单独的文件中)不能识别基类!!! ???

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'

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 namespaces and (ES6) modules at the same time. 但是,如果目的是组织代码,则同时使用名称空间(ES6)模块是一种不好的做法。 With Node.js, your files are modules, then you should avoid namespaces. 使用Node.js,您的文件是模块,那么您应该避免命名空间。

Use ES6 modules without 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 commonjs module syntax in compiler options). 它将适用于Node.js(在编译器选项中使用commonjs模块语法)。

For a good introduction to the ES6 modules syntax, read this article . 有关ES6模块语法的详细介绍,请阅读本文

Use a file tsconfig.json instead of /// <reference 使用文件tsconfig.json而不是/// <reference

Notice: The syntax /// <reference is replaced by the file tsconfig.json . 注意:语法/// <reference文件tsconfig.json替换。 An example for Node.js: Node.js的一个例子:

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

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

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