【问题标题】:Declare function with space inside a typings file在类型文件中用空格声明函数
【发布时间】:2020-05-26 15:29:39
【问题描述】:

有没有一种格式可以在 .d.ts 文件中声明名称中带有空格的函数?

某种形式的东西:

export namespace Foo {
    function "foo bar"(): void;
}

【问题讨论】:

    标签: types typescript-typings .d.ts


    【解决方案1】:

    您不能直接创建带有空格的函数声明。您可以使用 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 
    

    Playground Link

    注意:避免在新代码中使用命名空间,而是使用模块。

    【讨论】:

    • 命名空间和模块有什么区别?
    • namespaces 是用于代码分离的旧 TS 结构。模块(不要与 ts 中的 module 关键字混淆)是 ES2015 组织代码的标准方式
    • 谢谢,我认为.d.ts 文件的正确语法是:static ["foo bar"]: () => void;?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多