命名空间的 Typescript 概念
Typescript concept on namespace
我对打字稿命名空间的概念有点困惑。来自 Java,我猜命名空间跨越多个文件,并将它们分组到一个命名空间中。
但是在打字稿中,我不能将命名空间放入超过 1 个文件中,然后将它们全部导入。例如:
spaceConverter.ts
export namespace MySpace {
export class SpaceConverter1 {
}
}
timeConverter.ts
export namespace MySpace {
export class TimeConverter1 {
}
}
然后,如果我像
一样导入它们
import { MySpace } from './space';
import { MySpace } from './time';
我会收到重复标识符的错误,这很明显,
但如果我把它写成
export * from './space';
export * from './time';
我会知道 MySpace 已经导出了。
其实我的目标很简单..
我有多个 类,我想将它们放入 "MySpace" 组中,然后让应用程序导入所有这些,并使用一个名称,例如:
import { MySpace } from './myNameSpace';
var space = new MySpace.SpaceConverter1();
var time = new MySpace.TimeConverter1();
我只想要在 Typescript 中执行此操作的正确形式。
谢谢
命名空间在您不使用模块时很有用 (export/import)。它允许不使用大量函数污染全局范围,并防止覆盖全局范围内的现有代码。
模块已经隔离,因此它们不会与其他代码冲突。因此,您不需要在模块中使用命名空间。相反,您可以只导出 类 和函数。如果您想将多个模块组合成一个模块,您可以创建一个导出所有其他模块的新模块。这是一个例子:
// spaceConverter.ts
export class SpaceConverter1 {
}
// timeConverter.ts
export class TimeConverter1 {
}
// MySpace.ts
export * from './spaceConverter';
export * from './timeConverter';
// main.ts
import * as MySpace from './MySpace';
let converter = new MySpace.SpaceConverter1()
你真的不需要命名空间,因为模型本身提供了逻辑分组。
A key feature of modules in TypeScript is that two different modules
will never contribute names to the same scope. Because the consumer of
a module decides what name to assign it, there’s no need to
proactively wrap up the exported symbols in a namespace.
To reiterate why you shouldn’t try to namespace your module contents,
the general idea of namespacing is to provide logical grouping of
constructs and to prevent name collisions. Because the module file
itself is already a logical grouping, and its top-level name is
defined by the code that imports it, it’s unnecessary to use an
additional module layer for exported objects
从模块声明文件中删除名称空间并简化消费者代码中的代码。示例代码:
import { SpaceConverter1 } from './space';
import { TimeConverter1 } from './time';
var space = new SpaceConverter1();
var time = new TimeConverter1();
您也可以如上所述在 consumer class 中重命名模块。
示例:
重命名导入
import { SpaceConverter1 as SC} from "./space";
let converter = new SC();
在一个变量中导入整个模块
import * as SC from './space';
let converter = new SC.SpaceConverter1();
我对打字稿命名空间的概念有点困惑。来自 Java,我猜命名空间跨越多个文件,并将它们分组到一个命名空间中。
但是在打字稿中,我不能将命名空间放入超过 1 个文件中,然后将它们全部导入。例如:
spaceConverter.ts
export namespace MySpace {
export class SpaceConverter1 {
}
}
timeConverter.ts
export namespace MySpace {
export class TimeConverter1 {
}
}
然后,如果我像
一样导入它们import { MySpace } from './space';
import { MySpace } from './time';
我会收到重复标识符的错误,这很明显, 但如果我把它写成
export * from './space';
export * from './time';
我会知道 MySpace 已经导出了。
其实我的目标很简单.. 我有多个 类,我想将它们放入 "MySpace" 组中,然后让应用程序导入所有这些,并使用一个名称,例如:
import { MySpace } from './myNameSpace';
var space = new MySpace.SpaceConverter1();
var time = new MySpace.TimeConverter1();
我只想要在 Typescript 中执行此操作的正确形式。
谢谢
命名空间在您不使用模块时很有用 (export/import)。它允许不使用大量函数污染全局范围,并防止覆盖全局范围内的现有代码。
模块已经隔离,因此它们不会与其他代码冲突。因此,您不需要在模块中使用命名空间。相反,您可以只导出 类 和函数。如果您想将多个模块组合成一个模块,您可以创建一个导出所有其他模块的新模块。这是一个例子:
// spaceConverter.ts
export class SpaceConverter1 {
}
// timeConverter.ts
export class TimeConverter1 {
}
// MySpace.ts
export * from './spaceConverter';
export * from './timeConverter';
// main.ts
import * as MySpace from './MySpace';
let converter = new MySpace.SpaceConverter1()
你真的不需要命名空间,因为模型本身提供了逻辑分组。
A key feature of modules in TypeScript is that two different modules will never contribute names to the same scope. Because the consumer of a module decides what name to assign it, there’s no need to proactively wrap up the exported symbols in a namespace.
To reiterate why you shouldn’t try to namespace your module contents, the general idea of namespacing is to provide logical grouping of constructs and to prevent name collisions. Because the module file itself is already a logical grouping, and its top-level name is defined by the code that imports it, it’s unnecessary to use an additional module layer for exported objects
从模块声明文件中删除名称空间并简化消费者代码中的代码。示例代码:
import { SpaceConverter1 } from './space';
import { TimeConverter1 } from './time';
var space = new SpaceConverter1();
var time = new TimeConverter1();
您也可以如上所述在 consumer class 中重命名模块。 示例:
重命名导入
import { SpaceConverter1 as SC} from "./space";
let converter = new SC();
在一个变量中导入整个模块
import * as SC from './space';
let converter = new SC.SpaceConverter1();