Challenge - 5 Problems
Parameter Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of class with parameter properties shorthand
What is the output of this TypeScript code when compiled to JavaScript and run?
Typescript
class Person { constructor(public name: string, private age: number) {} greet() { return `Hello, my name is ${this.name}`; } } const p = new Person('Alice', 30); console.log(p.greet());
Attempts:
2 left
💡 Hint
Remember that parameter properties automatically create and initialize class members.
✗ Incorrect
The constructor parameters with 'public' and 'private' create class properties automatically. The greet method accesses 'this.name' which is set to 'Alice'. So the output is 'Hello, my name is Alice'.
🧠 Conceptual
intermediate1:30remaining
Understanding parameter properties shorthand effect on class members
Which statement about parameter properties shorthand in TypeScript is correct?
Attempts:
2 left
💡 Hint
Think about what happens when you add 'public' or 'private' before a constructor parameter.
✗ Incorrect
Parameter properties shorthand declares and initializes class members directly from constructor parameters, so you don't need separate property declarations or assignments.
🔧 Debug
advanced2:00remaining
Identify the error in parameter properties usage
What error will this TypeScript code produce?
Typescript
class Car { constructor(readonly model: string, speed: number) {} getModel() { return this.model; } } const c = new Car('Tesla', 120); console.log(c.getModel());
Attempts:
2 left
💡 Hint
Check which constructor parameters become class properties and which do not.
✗ Incorrect
There is no error. The 'readonly model' parameter creates a readonly class property 'model'. The 'speed' parameter lacks an access modifier so it does not create a class property, but since 'speed' is not accessed via 'this.speed', no error occurs. The code outputs 'Tesla'.
📝 Syntax
advanced1:30remaining
Which constructor uses parameter properties shorthand correctly?
Select the constructor that correctly uses parameter properties shorthand to declare and initialize class members.
Attempts:
2 left
💡 Hint
Parameter properties shorthand requires access modifiers on parameters and no manual assignments.
✗ Incorrect
Option D uses 'public' and 'private' modifiers on parameters and has an empty constructor body, which is the correct shorthand usage.
🚀 Application
expert2:00remaining
Count class members created by parameter properties shorthand
How many class members are created by this constructor using parameter properties shorthand?
Typescript
class Book {
constructor(public title: string, private author: string, year: number, readonly pages: number) {}
}Attempts:
2 left
💡 Hint
Only parameters with access modifiers become class members.
✗ Incorrect
Only 'title' (public), 'author' (private), and 'pages' (readonly) become class members. 'year' has no modifier, so it is just a parameter and not a member.