Syntax
class Name {
property: type;
constructor(param: type) { }
}Examples
Typed Properties and Constructor
Declaring class fields with explicit types, initialized via the constructor.
class User {
id: number;
name: string;
email: string;
constructor(id: number, name: string, email: string) {
this.id = id;
this.name = name;
this.email = email;
}
describe(): string {
return `${this.name} (${this.email})`;
}
}
const user = new User(1, "Alice", "alice@example.com");
console.log(user.describe());Parameter Properties Shorthand
A concise way to declare and assign class properties directly in the constructor signature.
class Product {
constructor(
public id: number,
public name: string,
private price: number
) {
// TypeScript automatically creates and assigns these three properties -
// no need to repeat "this.id = id" etc manually
}
getPrice(): number {
return this.price;
}
}
const product = new Product(1, "Laptop", 999);
console.log(product.name); // accessible - public
// console.log(product.price); // Error - privateBest practices
- Use parameter properties (public/private directly in the constructor signature) to reduce repetitive boilerplate for simple classes
- Type every class property explicitly, or initialize it with a value TypeScript can infer from - uninitialized untyped properties default to any implicitly, weakening safety
- Keep constructors focused on initialization - complex setup logic is often clearer in a separate method or factory function
- Use readonly on properties that should be set once in the constructor and never modified afterward
At a glance
- Purpose
- Static types for JavaScript
- File extension
- .ts ยท .tsx
- Runs in
- Compiled to JavaScript
- Usually used with
- JavaScript and its ecosystem
Specifications & further reading
Related TypeScript documentation
Access Modifiers: public, private, protected
Access modifiers control visibility of class members. public (the default) is accessible from anywhere. private restricts access to within the declaring class only, not even subclasses. protected allows access within the class and its subclasses, but not from outside. These are compile-time only checks - true runtime privacy needs the # syntax instead.Abstract Classes
An abstract class cannot be instantiated directly - it exists only to be extended. It can define abstract methods (a signature with no implementation, which subclasses must provide) alongside regular, fully-implemented methods that subclasses inherit as-is. This is TypeScript's way of enforcing a shared contract across a family of related classes.implements Keyword
The implements keyword declares that a class must conform to a specific interface's shape - TypeScript checks that every property and method the interface requires is actually present with a compatible type. Unlike extends, implements does not provide any inherited implementation; it is purely a compile-time contract check.Decorators
Decorators are functions that can observe, modify, or replace a class, method, property, or accessor, applied with @decoratorName syntax. They became a genuinely stable, standard part of TypeScript with TC39 Stage 3 decorator support landing as of TypeScript 5.9, and are the mechanism behind frameworks like Angular and NestJS for dependency injection and metadata-driven APIs.
Access modifiers control visibility of class members. public (the default) is accessible from anywhere. private restricts access to within the declaring class only, not even subclasses. protected allows access within the class and its subclasses, but not from outside. These are compile-time only checks - true runtime privacy needs the # syntax instead.Abstract Classes
An abstract class cannot be instantiated directly - it exists only to be extended. It can define abstract methods (a signature with no implementation, which subclasses must provide) alongside regular, fully-implemented methods that subclasses inherit as-is. This is TypeScript's way of enforcing a shared contract across a family of related classes.implements Keyword
The implements keyword declares that a class must conform to a specific interface's shape - TypeScript checks that every property and method the interface requires is actually present with a compatible type. Unlike extends, implements does not provide any inherited implementation; it is purely a compile-time contract check.Decorators
Decorators are functions that can observe, modify, or replace a class, method, property, or accessor, applied with @decoratorName syntax. They became a genuinely stable, standard part of TypeScript with TC39 Stage 3 decorator support landing as of TypeScript 5.9, and are the mechanism behind frameworks like Angular and NestJS for dependency injection and metadata-driven APIs.