【问题标题】:Typescript Definition file: Duplicate interface members to implementing classes?打字稿定义文件:重复的接口成员到实现类?
【发布时间】:2017-12-01 11:23:09
【问题描述】:

我正在尝试编写一个紧凑的 typescript 定义文件,但是对于一个更大的项目,我在这样做时遇到了麻烦。

我的项目有很多由多个类实现的接口。

据我所知,我总是需要重新“实现”/重新声明类中接口的方法,如下所示:

declare module someModule {

    interface InterfaceOne {
        /**
        * Some lengthy description
        */
        someStuff():any;
        /**
        * Some lengthy description
        */
        moreStuff():any;
    }

    class OneClass implements InterfaceOne {
        /**
        * Some lengthy description
        */
        someStuff():any;
        /**
        * Some lengthy description
        */
        moreStuff():any;
        /**
        * Even more description
        */
        classStuff(): any;
    }

    class TwoClass implements InterfaceOne {
        /**
        * Some lengthy description
        */
        someStuff():any;
        /**
        * Some lengthy description
        */
        moreStuff():any;
        /**
        * Even more description
        */
        classStuff(): any;
    }
}   

如果我从接口中省略 someStuffmoreStuff 声明 我收到此错误:

错误 TS2137:Class TwoClass 声明了接口 InterfaceOne 但没有实现它:

所以我总是需要将所有声明复制到每个实现接口的类。

有没有办法解决这个问题?为什么我需要这样做?是否有充分的理由必须将所有接口的组合内容复制到库声明文件中的类主体?这只是一个声明,而不是实现,那么为什么我的声明 implements InterfaceOne 还不够呢?我也不需要将所有基类型成员从超类型复制到扩展类型,那么为什么接口会有所不同?

在我正在为这些接口编写定义文件的库中,实际上是 mixins,所以最后我的定义文件实际上比具有主体的原始实现要长!

编辑:发布后我发现this answer - 所以我的问题可能是重复的,尽管另一个问题是针对旧版本的 Typescript。

编辑: 我意识到这可能不是讨论这个问题的正确地方,我正在考虑删除这个问题。作为参考,我在this official typescript issue问了这个问题。

【问题讨论】:

    标签: typescript typescript1.6 definitelytyped


    【解决方案1】:

    已编辑

    declare module someModule {
    
        interface InterfaceOne {
            /**
             * Some lengthy description
             */
            someStuff():any;
            /**
             * Some lengthy description
             */
            moreStuff():any;
        }
    
       interface OneClass extends InterfaceOne {
           new(): OneClass;
           /**
             * Even more description
             */
            classStuff(): any;
       }
    
       interface TwoClass extends InterfaceOne {
           new(): TwoClass;
           /**
             * Even more description
             */
            classStuff(): any;
       }
    
    }
    

    (在公共汽车上做的,需要更多检查)

    【讨论】:

    • 谢谢,但另一个更关键的问题是这意味着OneClassTwoClass 不能再从另一个(可能不同的)类扩展。这就是接口的用途——由不同/不相关的类实现。我的库是纯 Javascript,即使没有重复,.d.ts 文件的大小也是 8MB,所以即使自动创建它仍然会很大。
    • 好的。有一个“纯”接口解决方案可以使用 Factory 作为构造函数和变量来描述这一点。这有点令人费解,但是当我有几分钟的时间时,我会尝试编辑答案
    • 完全编辑了我的答案。是的...类接口。检查这个:stackoverflow.com/questions/13407036/…
    • 感谢您的努力,不幸的是,这是我已经拥有(8 MB)并且我正试图摆脱的情况。因为如果我用接口建模所有东西,这是可行的,但是没有类意味着没有人可以再正确地扩展我的类了。我想尽可能精确地为我的库建模,以充分利用打字稿。没有类意味着没有子类化:-(
    • 好吧,没关系,但为了论证:你没有这种情况:没有重复是我的例子,我不明白你关于子类化的论点:这只是一个定义文件,你总是可以扩展一个接口(建模一个类)
    【解决方案2】:

    你尝试过接口/类合并吗?

    https://www.typescriptlang.org/docs/handbook/declaration-merging.html

    export interface Foo {
        bar: string;
    }
    
    export class Foo {
        constructor() { this.bar = 'foobar'; }
    }
    
    export interface AnotherFoo extends Foo {}
    
    export class AnotherFoo {
        foo = 'bar'; // add a foo member only to this class
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-19
      • 1970-01-01
      • 2012-12-28
      • 2019-03-26
      • 2016-04-04
      • 2018-05-24
      • 2017-06-29
      • 2017-08-31
      • 2014-02-17
      相关资源
      最近更新 更多