Classes
A TypeScript class is a JavaScript class plus types for its fields, constructor, and methods. The type of an instance is the public instance shape; the class itself also has a construct signature.
Detailed Explanation
TypeScript classes emit as JavaScript classes (or as constructor functions if you target a very old target). The extra pieces are types: parameter annotations, field types, implements, abstract, visibility, readonly, parameter properties, and override. Those extra pieces are erased. instanceof still works, because a class is a real runtime value. An interface is not.
A class declaration creates two types. The instance type is what you get from new User(...) and from typeof a variable holding an instance. The constructor type is typeof User, which has new (...args) and the static members. Confusing the two is the usual error: function f(cls: User) wants an instance; function f(cls: typeof User) wants the class.
Fields may be declared with a type and an initializer, or left to the constructor. Under strictPropertyInitialization, every field that is not optional and does not have an initializer must be assigned in the constructor (or marked with a definite assignment assertion !, which is a claim you should be able to defend).
Methods are typed like functions. this inside a method is the instance type by default. Passing a method as a callback loses this at runtime — the same JavaScript rule — and TypeScript will catch some of those if you type the callback's this. Arrow-field methods close over this and cost an allocation per instance.
Classes are structurally typed. If two classes have the same public members, they are assignable in both directions even if they do not share a heritage. private and protected members break that: two classes with a private id are not assignable to each other, which is the closest TypeScript gets to nominal typing for classes. #private fields are a different mechanism and are truly private at runtime.
Use a class when you need instances, instanceof, inheritance, or stateful methods. Use an interface or type alias when you only need a shape. Use a type-only class (declare class) in a .d.ts when the implementation lives elsewhere.
Code Examples
1. Fields, constructor, and the two types a class creates.
// 1. Fields, constructor, and the two types a class creates.
class User {
readonly id: string;
email: string;
constructor(id: string, email: string) {
this.id = id;
this.email = email;
}
domain(): string {
return this.email.split("@")[1] ?? "";
}
}
const ada: User = new User("u1", "ada@example.com");
const Ctor: typeof User = User;
const copy: User = new Ctor("u2", "grace@example.com");
console.log(ada.domain()); // example.com2. Structural typing, and private members making it nominal.
// 2. Structural typing, and private members making it nominal.
class A { x = 1; }
class B { x = 1; }
const a: A = new B(); // fine — same public shape
class C { private id = 1; }
class D { private id = 1; }
// const c: C = new D();
// Error: types have separate declarations of a private property 'id'.3. strictPropertyInitialization.
// 3. strictPropertyInitialization.
class Session {
userId: string;
constructor(userId: string) {
this.userId = userId;
}
}Common Gotchas
`typeof User` is the constructor, not the instance
passing `User` where `User` (the instance type) is expected fails, and the error message names both.
Method callbacks lose `this`
extract an arrow, or wrap with `.bind`. The type of `this` will not save you if you used a function that types `this` as `any`.
Empty classes are assignable to everything in some positions
a class with no members is an empty shape. Give it a private field or a real member if you need identity.
`userId!` definite assignment is an assertion
the compiler will not prove the field is set. Prefer assigning in the constructor.