Recall & Review
beginner
What is a getter in TypeScript?
A getter is a special method that allows you to access the value of a property. It looks like a property but runs code when you get the value.
Click to reveal answer
beginner
What is a setter in TypeScript?
A setter is a special method that lets you set the value of a property. It runs code when you assign a value, allowing validation or other logic.
Click to reveal answer
intermediate
How do you specify the type of a getter's return value?
You add a return type after the getter method name, like
get propertyName(): Type { ... }.Click to reveal answer
intermediate
How do you type the parameter of a setter in TypeScript?
You specify the type of the single parameter in the setter method, like
set propertyName(value: Type) { ... }.Click to reveal answer
beginner
Why use getters and setters instead of public properties?
Getters and setters let you control how values are read or changed. You can add checks, format data, or trigger actions when values change.Click to reveal answer
How do you declare a getter for a property named
name returning a string in TypeScript?✗ Incorrect
A getter uses the
get keyword and specifies the return type after the method name.What is the correct way to type a setter for a property
age that accepts a number?✗ Incorrect
A setter uses the
set keyword and takes one typed parameter.Which of these is true about getters and setters in TypeScript?
✗ Incorrect
Getters and setters let you run code when properties are accessed or changed.
What happens if you try to set a property without a setter?
✗ Incorrect
If a property has only a getter, it is readonly and cannot be set.
How do you access a property with a getter in TypeScript?
✗ Incorrect
Getters look like normal properties when accessed.
Explain how to create a typed getter and setter for a property in TypeScript.
Think about how you define methods with types but use get/set keywords.
You got /6 concepts.
Why might you want to use getters and setters instead of public properties?
Consider how you protect or manage data in real life, like a safety lock.
You got /5 concepts.