tezvyn:

How do you write a declaration file for a JavaScript class?

AI-drafted, machine-checkedintermediate
WHAT IT TESTS

Ambient class declarations that model instance and static members of untyped JS.

ANSWER OUTLINE

Declare a class in a .d.ts, type the constructor, add instance members, and attach statics on the class.

WHAT THIS TESTS: This question probes your understanding of TypeScript's type declaration layer for untyped JavaScript libraries. Specifically, it checks whether you know how to emit an ambient class declaration that preserves the runtime distinction between the constructor function, the prototype, and static properties. Interviewers want to see that you understand instance versus static member resolution and that you can model a callable constructor with new without providing an implementation.

A GOOD ANSWER COVERS: First, the file must be a .d.ts ambient declaration file, typically using declare class or declare module to wrap the library. Second, the constructor signature must be explicitly typed with parameter types and optional access modifiers, because the constructor is what makes the class constructable with the new keyword. Third, instance properties and methods should be declared inside the class body without implementations, since this is a type declaration only. Fourth, static properties and methods must be declared with the static keyword inside the same class body, which places them on the constructor function type in TypeScript's type system. Fifth, a strong candidate mentions that if the class is exported from a module, the declaration should use export before declare class or wrap it in declare module with an export inside.

COMMON WRONG ANSWERS: A frequent mistake is declaring an interface instead of a class, which erases the constructor and makes new impossible. Another error is putting static members outside the class or treating them as global variables, which breaks the static scoping. Some candidates forget that .d.ts files contain no runtime code and try to write method bodies or assignments. Others conflate the declaration with an implementation file and attempt to import values rather than types. Finally, missing the distinction between readonly instance properties and mutable ones is a subtle red flag.

LIKELY FOLLOW-UPS: The interviewer may ask how you would type a class that is also callable without new, which requires combining a class with a callable interface or namespace merge. They might ask how to handle private or protected members in a declaration file, where you can use the private or protected keyword even without an implementation. Another follow-up is typing a class that extends another untyped class, requiring you to write extends and model inherited members. You might also be asked about generic classes, where the declaration introduces type parameters on the class itself.

ONE CONCRETE EXAMPLE: Imagine a JavaScript library exports a class named EventEmitter. In a declaration file you would write declare class EventEmitter, then an open brace, a constructor with an optional options parameter containing an optional captureRejections boolean, then instance methods like addListener taking an event string and a listener function returning this, and removeListener with the same signature, then static defaultMaxListeners as a number and static once taking an emitter instance and event string and returning a promise of an array. This gives consumers the constructor, instance methods, and static properties in one ambient class declaration.

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.