Parameter properties
Concept and syntax
A constructor parameter prefixed with public, private, protected or readonly becomes a parameter property. TypeScript generates instance initialization from the argument. This shorthand saves a separate field declaration and assignment; it is not merely a type annotation and can affect emitted code.
Working example
Save each snippet separately as main.ts in a fresh directory. Compile with tsc --strict --target ES2022 --module commonjs --moduleDetection force --skipLibCheck --noEmitOnError main.ts, then run the accepted example with node main.js. Forced module detection isolates snippet names from global declarations.
class Item {constructor(public readonly id:number,public label:string){}}const item=new Item(7,"tea");item.label="coffee";console.log(item.id,item.label);Expected output:
7 coffeeCommon mistake
Intentionally invalid; compile separately and do not execute.
class Item {constructor(public readonly id:number){}}const item=new Item(7);item.id=8;Expected diagnostic: TS2540. The parameter property is readonly. Set it during construction and create a replacement instance if needed.
Practice and recap
Expand the shorthand into explicit fields and constructor assignments; compare emitted JS.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Parameter properties — knowledge check
Check the contract, trace output, and diagnose an invalid example.
Which statement about this lesson’s concept is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
class Item {constructor(public readonly id:number,public label:string){}}const item=new Item(7,"tea");item.label="coffee";console.log(item.id,item.label);Which diagnostic and explanation match the rejected example under the lesson settings?
class Item {constructor(public readonly id:number){}}const item=new Item(7);item.id=8;