【问题标题】:Typescript: Using new in namespace打字稿:在命名空间中使用新的
【发布时间】:2020-05-28 02:37:18
【问题描述】:

我正在为私有包创建DefinitelyTyped(我无法更改源代码),但我找不到任何方法来实现这样的类型:

  GlobalNameSpace.SuperClass = function(arg) {}
  GlobalNameSpace.superClass = new GlobalNameSpace.SuperClass(args)

我的尝试:

declare namespace GlobalNameSpace {
    class SuperClass {}

    const superClass = new GlobalNameSpace.SuperClass(args);
}

遗憾的是,当我这样做时,VS Code 中出现错误。

A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.

知道如何解决这个问题吗?

【问题讨论】:

    标签: typescript definitelytyped


    【解决方案1】:

    这个怎么样:

    declare namespace GlobalNameSpace {
        export class SuperClass { }
        export const superClass: typeof SuperClass;
    }
    

    【讨论】:

    • 遗憾的是这并不能解决我的问题。当您执行GlobalNameSpace.superClass.sth 时,sth 出现错误。我的代码typescriptlang.org/play/…
    【解决方案2】:

    在@elderapo 在https://discord.gg/typescript 的帮助下修复了它

    最终解决方案:

    declare namespace GlobalNamespace {
        class SuperKomp {
            constructor();
            public on(key: string): void;
        }
    
        const komp: GlobalNamespace.SuperKomp;
    }
    
    GlobalNamespace.komp.on('click');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-13
      • 2017-11-13
      • 2016-07-24
      • 2016-11-20
      • 2018-08-06
      • 2021-09-20
      • 2017-02-09
      • 2021-02-01
      相关资源
      最近更新 更多