TypeScript · Types that pay for themselves
How do classes work in TypeScript?
Short answer
TypeScript classes add type annotations, access modifiers, parameter properties and implements clauses to JavaScript classes. Most syntax is erased, while fields and decorators can emit runtime code depending on configuration.
A TypeScript class is still a JavaScript class at runtime. Type annotations and access modifiers are erased. Parameter properties are a shorthand that declares and assigns constructor parameters in one place. The implements clause checks that the class satisfies an interface but does not add any inherited implementation.
interface Greeter {
greet(): string;
}
class User implements Greeter {
constructor(
public readonly id: number,
private name: string,
) {}
greet(): string {
return "Hello, " + this.name;
}
}
const user = new User(1, "Asha");What is type-only and what is runtime
- Type annotations, implements and access modifiers are erased
- Parameter properties emit assignments in the constructor
- Class fields emit runtime fields, with semantics affected by useDefineForClassFields
- Decorators can emit additional runtime code
- private is a compile-time check; hash-prefixed fields are runtime-private
The implements clause is a structural check, not inheritance. A class can implement several interfaces, but it only extends one base class. If the class changes in a way that no longer satisfies the interface, the compiler reports the mismatch at the class declaration.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is a parameter property?
- 2Does implements add inherited behavior?
- 3What is the difference between private and hash-prefixed fields?
- 4How does useDefineForClassFields affect runtime behavior?
Say it in the interview
A TypeScript class is a JavaScript class plus type annotations, access modifiers and implements. Parameter properties let me declare and assign constructor fields in one place. The implements clause checks that the class satisfies an interface but does not add inherited behavior. I use classes when I need identity and mutable state, and interfaces or plain objects when I only need a shape.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live TypeScript mock interview and get scored feedback on depth, clarity and confidence.