What it is and why to use it
A constructor parameter with an accessibility or readonly modifier can declare and initialise a class property at the same time. This TypeScript shorthand reduces repeated field declarations and assignments. It is useful for small data-bearing classes when the shorthand remains clear.
Common mistakes and limitations
TypeScript private is a compile-time visibility rule, not ECMAScript #private storage. Parameter properties require a TypeScript transformation and are not supported by every type-stripping-only runtime. The documented compiler workflow handles them.
Run this example
Use TypeScript 5.8 or later with strict checking and an ES2022 target. Save each complete example independently as example.ts, compile it, then run the generated JavaScript with a compatible Node.js installation. Compiler-only options below affect checking; they do not add runtime validation.
Terminal — compile and run
npx tsc example.ts --strict --target ES2022 --module commonjs
node example.jsSyntax
constructor(public name: string, private score: number) {}Examples
Constructor Parameter Properties
Standalone TypeScript 5.8+ example; expected output is explained below.
class Learner {
constructor(public name: string, private score: number) {}
summary(): string { return this.name + ": " + this.score; }
}
const learner = new Learner("John", 87);
console.log(learner.name);
console.log(learner.summary());- public name creates a property callers can access.
- private score creates a property checked as private by TypeScript; summary can read it inside the class.
- The compiler emits initialisation code. A plain constructor parameter without a parameter-property modifier would not automatically become an instance field.
- Expected output: John John: 87
Best practices
- TypeScript private is a compile-time visibility rule, not ECMAScript #private storage. Parameter properties require a TypeScript transformation and are not supported by every type-stripping-only runtime. The documented compiler workflow handles them.
- Keep external-input validation separate from compile-time type descriptions.
Runtime & versions
Compiled to JavaScript. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
TypeScript Handbook