parliament parliament - 9 days ago 4
TypeScript Question

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() {} }