This helps you write shorter code when creating classes. It lets you make and save properties in one step.
Parameter properties shorthand in Typescript
class ClassName { constructor(private propertyName: Type, public anotherProperty: Type) { // No need to write this.propertyName = propertyName; } }
Use public, private, or protected before the parameter to create a property automatically.
This shorthand only works inside the constructor parameters.
name as a public property and age as a private property.class Person {
constructor(public name: string, private age: number) {}
}brand as a protected property and year as a public property.class Car {
constructor(protected brand: string, public year: number) {}
}title.class Book {
constructor(public title: string) {}
}This program creates a User class using parameter properties shorthand. It sets username as public and password as private. Then it prints the username.
class User { constructor(public username: string, private password: string) {} showUsername() { console.log(`Username: ${this.username}`); } // Password is private, so no direct access outside } const user = new User('alice', 'secret123'); user.showUsername();
Parameter properties shorthand saves you from writing extra lines like this.property = property;.
Private and protected properties cannot be accessed outside the class.
Use this shorthand only for simple property assignments in constructors.
Parameter properties shorthand lets you create and assign class properties in one step inside the constructor.
Use public, private, or protected before constructor parameters to make properties.
This makes your class code shorter and easier to read.
Practice
Solution
Step 1: Understand parameter properties shorthand
Parameter properties shorthand uses access modifiers like public, private, or protected before constructor parameters to create and assign class properties automatically.Step 2: Compare with other options
Options B, C, and D describe unrelated features: global variables, getter/setter methods, and module imports, which are not related to parameter properties shorthand.Final Answer:
It creates and assigns class properties in one step using access modifiers. -> Option CQuick Check:
Parameter properties shorthand = create and assign properties [OK]
- Confusing shorthand with getter/setter methods
- Thinking it declares global variables
- Assuming it imports modules
Solution
Step 1: Identify correct parameter properties syntax
The correct syntax places an access modifier (public, private, protected) before the parameter name and type inside the constructor, likeconstructor(public name: string) { }.Step 2: Check other options for errors
constructor(name: string) { this.name = name; } lacks shorthand (no access modifier). constructor(private name) { this.name = name; } misses the type annotation. constructor(name: string public) { } has incorrect modifier placement after the type.Final Answer:
constructor(public name: string) { } -> Option AQuick Check:
Access modifier before parameter = correct syntax [OK]
- Placing access modifier after type
- Omitting type annotation
- Not using access modifier at all
class Person {
constructor(private name: string) {}
greet() {
return `Hello, ${this.name}!`;
}
}
const p = new Person('Anna');
console.log(p.greet());Solution
Step 1: Understand private parameter property
The constructor usesprivate name: string, which creates and assigns a private propertynameautomatically.Step 2: Check greet method output
Thegreet()method accessesthis.nameand returns "Hello, Anna!" because the property was set correctly.Final Answer:
Hello, Anna! -> Option DQuick Check:
Private property set by constructor = correct output [OK]
- Assuming private means inaccessible inside class
- Expecting undefined because no explicit assignment
- Confusing property with parameter
class Car {
constructor(public model) {}
}
const c = new Car('Tesla');
console.log(c.model);Solution
Step 1: Check parameter properties syntax
The constructor parametermodelusespublicbut lacks a type annotation, which is required in TypeScript.Step 2: Verify other options
Cannot use public modifier in constructor. is wrong because public modifier is allowed. Property 'model' is not accessible outside the class. is wrong because public makes property accessible. Constructor must have a return type. is wrong because constructors do not have return types.Final Answer:
Missing type annotation for parameter 'model'. -> Option BQuick Check:
Parameter properties require type annotations [OK]
- Omitting type annotation
- Thinking public is invalid in constructor
- Expecting constructor to have return type
Book that stores title and author as public properties using parameter properties shorthand. Which code correctly implements this and allows creating a Book instance with both properties set?Solution
Step 1: Identify correct use of parameter properties shorthand
Bothtitleandauthormust have access modifiers (public) before their parameters to create public properties automatically.Step 2: Evaluate each option
class Book { constructor(public title: string, author: string) {} } only makestitlepublic;authoris just a parameter, not a property. class Book { constructor(public title: string, public author: string) {} } correctly usespublicfor both. class Book { constructor(title: string, author: string) { this.title = title; this.author = author; } } does not use shorthand but tries to assign properties without declaring them, causing errors. class Book { constructor(title: string, public author: string) {} } only makesauthorpublic,titleis missing.Final Answer:
class Book { constructor(public title: string, public author: string) {} } -> Option AQuick Check:
Use public before all parameters to create properties [OK]
- Adding public to only some parameters
- Assigning properties without declaring them
- Mixing shorthand with manual assignments incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
