What is the parameter properties shorthand in TypeScript?
It is a way to declare and initialize class properties directly in the constructor parameters by adding an access modifier (like <code>public</code>, <code>private</code>, or <code>protected</code>) before the parameter name.
Click to reveal answer
beginner
How does parameter properties shorthand help when writing classes?
It reduces the amount of code by combining property declaration and assignment into one step inside the constructor, making the code shorter and easier to read.
Click to reveal answer
beginner
Rewrite this class using parameter properties shorthand:<br><pre>class Person {
name: string;
constructor(name: string) {
this.name = name;
}
}</pre>
<pre>class Person {
constructor(public name: string) {}
}</pre>
Click to reveal answer
intermediate
Which access modifiers can be used with parameter properties shorthand?
You can use public, private, protected, and readonly modifiers to control visibility and mutability of the property.
Click to reveal answer
beginner
True or False: Parameter properties shorthand can only be used with public parameters.
False. Parameter properties shorthand can be used with public, private, protected, and readonly parameters.
Click to reveal answer
What does the parameter properties shorthand do in TypeScript?
ADeclares and initializes class properties directly in constructor parameters
BCreates global variables automatically
CDefines functions inside constructor parameters
DRemoves the need for constructors
✗ Incorrect
Parameter properties shorthand lets you declare and initialize class properties in one step inside the constructor parameters.
Which of these is a valid use of parameter properties shorthand?
Aconstructor(private age: number) {}
Bconstructor(age: number) { this.age = age; }
Cconstructor(age) { this.age = age; }
Dconstructor(public age) { age = 10; }
✗ Incorrect
Using an access modifier like private before the parameter name declares and initializes the property automatically.
What happens if you omit the access modifier in a constructor parameter?
AThe parameter becomes a class property
BThe parameter is just a normal parameter, no property is created
CThe code will not compile
DThe parameter becomes a global variable
✗ Incorrect
Without an access modifier, the parameter is only a normal parameter and does not create a class property.
Can you use readonly with parameter properties shorthand?
AOnly with <code>public</code> modifier
BNo, <code>readonly</code> is not allowed
COnly with <code>private</code> modifier
DYes, to make the property immutable
✗ Incorrect
readonly can be used to make the property immutable after initialization.
Which of these is NOT a benefit of parameter properties shorthand?
ALess code to write
BCombines declaration and assignment
CAutomatically creates getter and setter methods
DImproves readability
✗ Incorrect
Parameter properties shorthand does not create getter or setter methods automatically.
Explain how parameter properties shorthand works and why it is useful.
Think about how you normally declare properties and assign them in the constructor.
You got /4 concepts.
Write a simple TypeScript class using parameter properties shorthand with at least two properties.
Use public or private before the parameter names in the constructor.
You got /3 concepts.
Practice
(1/5)
1. What does the parameter properties shorthand in TypeScript do inside a class constructor?
easy
A. It automatically generates getter and setter methods.
B. It declares global variables outside the class.
C. It creates and assigns class properties in one step using access modifiers.
D. It imports external modules into the class.
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 C
Quick Check:
Parameter properties shorthand = create and assign properties [OK]
Hint: Look for access modifiers before constructor parameters [OK]
Common Mistakes:
Confusing shorthand with getter/setter methods
Thinking it declares global variables
Assuming it imports modules
2. Which of the following is the correct syntax to use parameter properties shorthand in a TypeScript class constructor?
easy
A. constructor(public name: string) { }
B. constructor(name: string) { this.name = name; }
C. constructor(private name) { this.name = name; }
The correct syntax places an access modifier (public, private, protected) before the parameter name and type inside the constructor, like constructor(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 A
Quick Check:
Access modifier before parameter = correct syntax [OK]
Hint: Access modifier must come before parameter name and type [OK]
Common Mistakes:
Placing access modifier after type
Omitting type annotation
Not using access modifier at all
3. What will be the output of this TypeScript code?
class Person {
constructor(private name: string) {}
greet() {
return `Hello, ${this.name}!`;
}
}
const p = new Person('Anna');
console.log(p.greet());
medium
A. Hello, !
B. Hello, undefined!
C. Error: Property 'name' does not exist
D. Hello, Anna!
Solution
Step 1: Understand private parameter property
The constructor uses private name: string, which creates and assigns a private property name automatically.
Step 2: Check greet method output
The greet() method accesses this.name and returns "Hello, Anna!" because the property was set correctly.
Final Answer:
Hello, Anna! -> Option D
Quick Check:
Private property set by constructor = correct output [OK]
Hint: Private properties set by constructor are accessible inside class [OK]
Common Mistakes:
Assuming private means inaccessible inside class
Expecting undefined because no explicit assignment
Confusing property with parameter
4. Identify the error in this TypeScript class using parameter properties shorthand:
class Car {
constructor(public model) {}
}
const c = new Car('Tesla');
console.log(c.model);
medium
A. Property 'model' is not accessible outside the class.
B. Missing type annotation for parameter 'model'.
C. Cannot use public modifier in constructor.
D. Constructor must have a return type.
Solution
Step 1: Check parameter properties syntax
The constructor parameter model uses public but 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 B
Quick Check:
Parameter properties require type annotations [OK]
Hint: Always add type after parameter in constructor shorthand [OK]
Common Mistakes:
Omitting type annotation
Thinking public is invalid in constructor
Expecting constructor to have return type
5. You want to create a TypeScript class 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?
hard
A. class Book {
constructor(public title: string, public author: string) {}
}
B. class Book {
constructor(public title: string, author: string) {}
}
C. class Book {
constructor(title: string, author: string) {
this.title = title;
this.author = author;
}
}
D. class Book {
constructor(title: string, public author: string) {}
}
Solution
Step 1: Identify correct use of parameter properties shorthand
Both title and author must 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 makes title public; author is just a parameter, not a property. class Book {
constructor(public title: string, public author: string) {}
} correctly uses public for 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 makes author public, title is missing.
Final Answer:
class Book {
constructor(public title: string, public author: string) {}
} -> Option A
Quick Check:
Use public before all parameters to create properties [OK]
Hint: Add public before every parameter to create properties [OK]
Common Mistakes:
Adding public to only some parameters
Assigning properties without declaring them
Mixing shorthand with manual assignments incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.