【发布时间】:2020-05-26 15:29:39
【问题描述】:
有没有一种格式可以在 .d.ts 文件中声明名称中带有空格的函数?
某种形式的东西:
export namespace Foo {
function "foo bar"(): void;
}
【问题讨论】:
标签: types typescript-typings .d.ts
有没有一种格式可以在 .d.ts 文件中声明名称中带有空格的函数?
某种形式的东西:
export namespace Foo {
function "foo bar"(): void;
}
【问题讨论】:
标签: types typescript-typings .d.ts
您不能直接创建带有空格的函数声明。您可以使用 namespace-class 合并来实现您想要的:
export class Foo {
private constructor() { } // so nobody acidentaly instantiates this
static ["foo-bar"] = function (): void {
}
}
export namespace Foo {
export function other() {
}
}
Foo.other() // ok
Foo["foo-bar"]() // ok
注意:避免在新代码中使用命名空间,而是使用模块。
【讨论】:
namespaces 是用于代码分离的旧 TS 结构。模块(不要与 ts 中的 module 关键字混淆)是 ES2015 组织代码的标准方式
.d.ts 文件的正确语法是:static ["foo bar"]: () => void;?