解决方案与命名空间(不建议)
要解决您的问题,您可以导出命名空间:
// UtilBase.ts
import * as path from "path";
export namespace My.utils {
export class UtilBase {
protected fixPath(value: string): string {
return value.replace('/', path.sep);
}
}
}
然后,您应该能够导入它:
// UtilOne.ts
import {My} from './UtilBase';
namespace My.utils {
export class UtilOne extends My.utils.UtilBase {
}
}
但是,如果目的是组织代码,这是一个不好的做法命名空间 and (ES6) 模块同时 https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html#needless-namespacing。对于 Node.js,您的文件是模块,那么您应该避免命名空间。
Use ES6模块没有命名空间
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 {
}
这是推荐的方式。 ES6 模块可以通过重命名每个导入的资源来防止命名冲突。
它将在 Node.js 上运行。
有关 ES6 模块语法的详细介绍,阅读这篇文章 https://hacks.mozilla.org/2015/08/es6-in-depth-modules/.
使用文件tsconfig.json
代替/// <reference
注意:语法/// <reference
被替换为文件tsconfig.json https://www.typescriptlang.org/docs/handbook/tsconfig-json.html。 Node.js 的示例:
// tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es6"
},
"exclude": [
"node_modules"
]
}