Constructor parameter types tell TypeScript what kind of values a class needs when it starts. This helps catch mistakes early and makes your code safer.
Constructor parameter types in Typescript
class ClassName { constructor(parameterName: Type) { // use parameterName inside the class } }
The constructor is a special method that runs when you create a new object from the class.
Adding : Type after the parameter name tells TypeScript what type to expect.
string called name when you create a new Person.class Person { constructor(name: string) { console.log(`Hello, ${name}!`); } }
x and y, to create a point.class Point { constructor(x: number, y: number) { console.log(`Point at (${x}, ${y})`); } }
id (number), name (string), and price (number) to create a product.class Product { constructor(id: number, name: string, price: number) { console.log(`Product: ${name}, Price: $${price}`); } }
This program creates a Car class that needs a make (string) and year (number) when you create it. Then it shows the car details.
class Car { make: string; year: number; constructor(make: string, year: number) { this.make = make; this.year = year; } display() { console.log(`Car: ${this.make}, Year: ${this.year}`); } } const myCar = new Car("Toyota", 2020); myCar.display();
If you pass the wrong type to the constructor, TypeScript will show an error before running the code.
You can add multiple parameters with different types to the constructor.
Constructor parameters help make your classes clear and safe to use.
Constructor parameter types tell what kind of values a class needs when created.
They help catch mistakes early by checking types before running the code.
Use them to make your classes clear and easy to understand.
Practice
Solution
Step 1: Understand constructor parameter types
Constructor parameter types define what type of values the constructor expects to receive when creating an instance of the class.Step 2: Identify the purpose of parameter types
They help TypeScript check for errors before running the code by ensuring the right types are passed.Final Answer:
It tells what kind of values the constructor expects when creating an object. -> Option DQuick Check:
Constructor parameter types = Define expected input types [OK]
- Thinking parameter types speed up runtime
- Confusing parameter types with method creation
- Believing parameter types rename classes
Solution
Step 1: Review TypeScript constructor syntax
In TypeScript, parameter types are declared after the parameter name with a colon, likename: string.Step 2: Check each option
constructor(name: string, age: number) {} uses correct syntax with colons and types. Options A, C, and D use invalid syntax.Final Answer:
constructor(name: string, age: number) {} -> Option AQuick Check:
Parameter types use colon syntax [OK]
- Omitting colons between name and type
- Using commas or arrows instead of colons
- Placing types after parentheses
class Person {
constructor(public name: string, private age: number) {}
getAge() { return this.age; }
}
const p = new Person('Alice', 30);
console.log(p.name);
console.log(p.getAge());Solution
Step 1: Understand public and private in constructor
Thepublic namecreates a public property accessible outside. Theprivate agecreates a private property accessible only inside the class.Step 2: Check console.log outputs
p.nameprints 'Alice' because it's public.p.getAge()returns 30 because the method accesses privateageinternally.Final Answer:
Alice 30 -> Option BQuick Check:
Public accessible, private via method [OK]
- Expecting private properties accessible outside
- Confusing undefined with private access
- Thinking private causes runtime error
class Car {
constructor(make: string, year: number) {
this.make = make;
this.year = year;
}
}Solution
Step 1: Check class properties and constructor assignments
Step 2: Check class properties
The class does not declare properties 'make' and 'year' before assigning them in the constructor, which causes an error: Property 'make' does not exist on type 'Car'.Final Answer:
Class properties 'make' and 'year' are missing -> Option CQuick Check:
Declare class properties before use [OK]
- Ignoring missing property declarations
- Thinking constructor parameters auto-create properties
- Expecting return type in constructor
Book that takes a title (string), pages (number), and an optional author (string). Which constructor declaration correctly types these parameters and allows creating a Book by omitting the author argument (leaving author as undefined)?Solution
Step 1: Understand optional parameters in TypeScript
Optional parameters are marked with a question mark?after the parameter name, allowing them to be omitted when calling the constructor, leaving the parameterundefined.Step 2: Evaluate each option
constructor(title: string, pages: number, author?: string = '') {}:author?: string = ''-- omitting sets to'', notundefined. constructor(title: string, pages: number, author: string = '') {}:author: string = ''-- omitting sets to''. constructor(title: string, pages: number, author: string) {} requiresauthor. constructor(title: string, pages: number, author?: string) {}:author?: string-- omitting leavesundefined.Final Answer:
constructor(title: string, pages: number, author?: string) {} -> Option AQuick Check:
Optional params use ? after name [OK]
- Using default value without ? for optional
- Forgetting to mark optional parameters
- Mixing optional and default syntax incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
