Back to All Sheets
TypeScriptv5.4INTERMEDIATE

TypeScript Class & Type System

Essential syntax reference for TypeScript classes, generic constraints, type-only modifiers, control flow analysis, and type evaluation.

12 min read
Category: Frontend

Class Declarations

Field modifiers and constructor parameter properties.

Parameter Properties & Modifiers

Automatic instance field initialization via constructor parameters.

TypeScript Class Member Modifiers
ModifierAccess / EffectRuntime EnforcementTypical Use
publicAccessible everywhere; defaultNormal JavaScript propertyPublic class API
protectedClass and subclassesType-checker onlyExtension points for derived classes
privateDeclaring class onlyType-checker onlyCompile-time encapsulation
#privateDeclaring class onlyYes; JavaScript private fieldHard runtime encapsulation
readonlyCannot be reassigned after initializationType-checker onlyStable reference or value
staticMember belongs to the class constructorYesFactories, constants, shared utilities
typescript
class User extends Account implements Updatable {
id: string;
readonly createdAt = new Date();
constructor(public email: string) {
super();
}
}
Code Annotations:
extends Account

Subclasses base Account class

public email: string

Parameter property automatically sets instance field

Control Flow & Narrowing

Type guard functions and predicate assertions.

Type Predicates & Narrowing

Custom type assertion functions with obj is Type return signatures.

typescript
function isErrorResponse(obj: Response): obj is APIErrorResponse {
return obj instanceof APIErrorResponse;
}
if (isErrorResponse(res)) {
console.error(res.message);
}
Code Annotations:
obj is APIErrorResponse

Type predicate assertion for scope narrowing