简体   繁体   中英

Typescript declaration files contain class definitions with no member implementations

When I look at the Backbone declaration file I see this:

export class Router extends Events {
    ...
    constructor (options?: RouterOptions);
    initialize (options?: RouterOptions);
    ...
}

and other places where a class has method definitions with no implementations. I thought this syntax was reserved for interfaces. The compiler lets this pass in the declaration file but not in my own ts files.

Is there a difference between the compilation rules for .d.ts vs .ts extensions? If so, how should these types of classes be used differently from interfaces?

.d.ts files are for describing an existing JavaScript or TypeScript implementation of some class.

A class in a .d.ts (I'll just call this a " declare class " since they are equivalent) is completely different from a virtual class or an interface. When you declare a declare class , you're saying "There is some other class that will be present that has this shape". When you extend that class, the compiler is going to emit code on the assumption that there really will be a class (or sufficiently classlike thing) with that name present at runtime to use as the the next pointer in the prototype chain.

Just as an example, this code (by itself) does not work - you will get a runtime error because Foo isn't defined anywhere:

declare class Foo {  public bar(): void; }
class FooDerived extends Foo { }

This code, on the other hand, is fine:

interface Foo { bar(): void; }
class FooImpl implements Foo { public bar() {} }

If the file has a .d.ts extension, all of the classes are treated as if they were preceded by a declare keyword. Declarations don't need an implementation, they just supply type information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM