What's the difference between “declare class” and “interface” in TypeScript

后端 未结 4 2263
谎友^
谎友^ 2020-11-27 10:33

In TypeScript, when creating .d.ts source declaration files, which is preferable and why?

declare class Example {
    public Method(): void; 
}
4条回答
  •  青春惊慌失措
    2020-11-27 11:28

    You can implement the interface:

    class MyClass implements Example {
        Method() {
    
        }
    }
    

    Whereas the declare class syntax is really intended to be used to add type definitions for external code that isn't written in TypeScript - so the implementation is "elsewhere".

提交回复
热议问题