TypeScript Classes and Modules/Readonly class fields

Prevent reassignment after construction.

Section: Classes

Readonly class fields

typescript
typescript
class Config {
  readonly env: string;

  constructor(env: string) {
    this.env = env;
  }
}
Explanation

Readonly is useful for IDs, config values, and immutable domain objects.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Classes
Define a class
Type instance fields and constructor parameters.
OpenIn sheettypescriptsame section
Use access modifiers
Control visibility with `public`, `private`, and `protected`.
OpenIn sheettypescriptsame section
Extend a class
Reuse behavior with inheritance.
OpenIn sheettypescriptsame section
Use an abstract class
Require derived classes to implement specific methods.
OpenIn sheettypescriptsame section
Use getters and setters
Wrap computed or validated access around fields.
OpenIn sheettypescriptsame section
Named export
Export multiple values from a module.
OpenIn sheettypescript1 tag match