What is the purpose of specifying parameter types in a constructor in TypeScript?
Specifying parameter types in a constructor helps ensure that the values passed when creating an object are of the expected types, preventing errors and improving code clarity.
Click to reveal answer
beginner
How do you define a constructor with typed parameters in a TypeScript class?
You declare the constructor with parameters followed by a colon and the type, for example:
constructor(name: string, age: number) { ... }
Click to reveal answer
intermediate
Can constructor parameters in TypeScript be optional? How?
Yes, by adding a question mark after the parameter name, e.g.,
constructor(name?: string) { ... }
, making the parameter optional.
Click to reveal answer
beginner
What happens if you pass a wrong type to a constructor parameter in TypeScript?
TypeScript will show a compile-time error, preventing the code from running until the type mismatch is fixed.
Click to reveal answer
intermediate
How can you automatically create and assign class properties from constructor parameters in TypeScript?
By adding access modifiers (public, private, protected, readonly) before the constructor parameters, TypeScript creates and assigns properties automatically, e.g.,
constructor(public name: string) { }
Click to reveal answer
How do you specify that a constructor parameter 'age' must be a number in TypeScript?
Aconstructor(age: number) {}
Bconstructor(age) {}
Cconstructor(age: string) {}
Dconstructor(age = number) {}
✗ Incorrect
Option A correctly declares 'age' as a number type parameter.
What does adding a question mark after a constructor parameter name do?
AThrows a syntax error
BMakes the parameter required
CMakes the parameter optional
DChanges the parameter type to boolean
✗ Incorrect
A question mark after a parameter name makes it optional.
Which of these will automatically create a class property from a constructor parameter?
Aconstructor(name: string) { this.name = name; }
Bconstructor(public name: string) {}
Cconstructor(name) {}
Dconstructor(private) {}
✗ Incorrect
Using 'public' before the parameter automatically creates and assigns the property.
What error will TypeScript show if you pass a string to a constructor expecting a number?
AType error: string is not assignable to number
BSyntax error
CRuntime error
DNo error
✗ Incorrect
TypeScript shows a type error at compile time for type mismatches.
Which syntax correctly defines a constructor with an optional string parameter 'title'?
Aconstructor(optional title: string) {}
Bconstructor(title: string?) {}
Cconstructor(title = string) {}
Dconstructor(title?: string) {}
✗ Incorrect
Option D uses the correct syntax for an optional parameter.
Explain how constructor parameter types improve code safety and readability in TypeScript.
Think about what happens when you pass wrong types to constructors.
You got /4 concepts.
Describe how to use access modifiers in constructor parameters to create class properties automatically.
Focus on the shortcut TypeScript offers inside constructors.
You got /4 concepts.
Practice
(1/5)
1. What does specifying parameter types in a TypeScript class constructor do?
easy
A. It changes the class name based on parameters.
B. It runs the constructor faster at runtime.
C. It automatically creates getter and setter methods.
D. It tells what kind of values the constructor expects when creating an object.
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 D
2. Which of the following is the correct way to declare a constructor with typed parameters in TypeScript?
easy
A. constructor(name: string, age: number) {}
B. constructor(name string, age number) {}
C. constructor(name, age) : string, number {}
D. constructor(name => string, age => number) {}
Solution
Step 1: Review TypeScript constructor syntax
In TypeScript, parameter types are declared after the parameter name with a colon, like name: 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 A
Quick Check:
Parameter types use colon syntax [OK]
Hint: Use colon after parameter name for types [OK]
Common Mistakes:
Omitting colons between name and type
Using commas or arrows instead of colons
Placing types after parentheses
3. What will be the output of this TypeScript code?
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());
medium
A. Error: age is private
B. Alice
30
C. Alice
undefined
D. undefined
30
Solution
Step 1: Understand public and private in constructor
The public name creates a public property accessible outside. The private age creates a private property accessible only inside the class.
Step 2: Check console.log outputs
p.name prints 'Alice' because it's public. p.getAge() returns 30 because the method accesses private age internally.
Final Answer:
Alice
30 -> Option B
Quick Check:
Public accessible, private via method [OK]
Hint: Public props accessible directly, private via methods [OK]
Common Mistakes:
Expecting private properties accessible outside
Confusing undefined with private access
Thinking private causes runtime error
4. Identify the error in this TypeScript class constructor:
class Car {
constructor(make: string, year: number) {
this.make = make;
this.year = year;
}
}
medium
A. Constructor must have return type declared
B. Cannot assign parameters to 'this' inside constructor
C. Class properties 'make' and 'year' are missing
D. Missing type for parameter 'year' in constructor
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 C
Quick Check:
Declare class properties before use [OK]
Hint: Declare properties before assigning in constructor [OK]
5. You want to create a class 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)?
hard
A. constructor(title: string, pages: number, author?: string) {}
B. constructor(title: string, pages: number, author: string = '') {}
C. constructor(title: string, pages: number, author: string) {}
D. constructor(title: string, pages: number, author?: string = '') {}
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 parameter undefined.