【问题标题】:Typescript prototyping of a definition file定义文件的打字稿原型
【发布时间】:2017-04-26 05:39:43
【问题描述】:

我正在学习 Typescript,我想利用原型设计。我还使用了一个外部库,它有一个类型文件。

到目前为止,这是可行的。

// main.ts
declare class Foo {}

interface Foo {
  bar(): void;
}

Foo.prototype.bar = function() {}

但是,声明在不同的文件中,所以当我尝试这个时它不起作用。

foo.d.ts:

declare class Foo {}

main.ts:

interface Foo {
  bar(): void;
}

Foo.prototype.bar = function() {}

main.ts: TS6133: 'Foo' 已声明但从未使用过。
main.ts:TS2339:“Foo”类型上不存在属性“bar”。

是否有我缺少的编译标志或什么?在单独的文件中如何编译?

【问题讨论】:

    标签: typescript prototype


    【解决方案1】:

    你需要使用module augmentation:

    // main.ts
    import { Foo } from "./foo";
    
    Foo.prototype.bar = function() {}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-02
      • 2017-02-20
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 2018-11-06
      相关资源
      最近更新 更多