Bird
Raised Fist0
Typescriptprogramming~20 mins

Parameter properties shorthand in Typescript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Parameter Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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());
AHello, my name is undefined
BHello, my name is Alice
CTypeError: p.greet is not a function
DCompilation error due to private modifier
Attempts:
2 left
💡 Hint
Remember that parameter properties automatically create and initialize class members.
🧠 Conceptual
intermediate
1:30remaining
Understanding parameter properties shorthand effect on class members
Which statement about parameter properties shorthand in TypeScript is correct?
AIt can only be used with public access modifier.
BIt only declares class members but does not initialize them.
CIt declares and initializes class members from constructor parameters automatically.
DIt requires explicit property declarations outside the constructor.
Attempts:
2 left
💡 Hint
Think about what happens when you add 'public' or 'private' before a constructor parameter.
🔧 Debug
advanced
2: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());
ANo error, outputs 'Tesla'
BProperty 'speed' is not initialized and causes an error
CCannot use 'readonly' in constructor parameters
DMethod getModel is not defined
Attempts:
2 left
💡 Hint
Check which constructor parameters become class properties and which do not.
📝 Syntax
advanced
1:30remaining
Which constructor uses parameter properties shorthand correctly?
Select the constructor that correctly uses parameter properties shorthand to declare and initialize class members.
Aconstructor(name: string, private age: number) { this.name = name; }
Bconstructor(name: string, age: number) { this.name = name; this.age = age; }
Cconstructor(private name: string, age: number) { this.age = age; }
Dconstructor(public name: string, private age: number) {}
Attempts:
2 left
💡 Hint
Parameter properties shorthand requires access modifiers on parameters and no manual assignments.
🚀 Application
expert
2: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) {}
}
A3
B4
C2
D1
Attempts:
2 left
💡 Hint
Only parameters with access modifiers become class members.

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

  1. 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.
  2. 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.
  3. Final Answer:

    It creates and assigns class properties in one step using access modifiers. -> Option C
  4. 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; }
D. constructor(name: string public) { }

Solution

  1. Step 1: Identify correct parameter properties syntax

    The correct syntax places an access modifier (public, private, protected) before the parameter name and type inside the constructor, like constructor(public name: string) { }.
  2. 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.
  3. Final Answer:

    constructor(public name: string) { } -> Option A
  4. 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

  1. Step 1: Understand private parameter property

    The constructor uses private name: string, which creates and assigns a private property name automatically.
  2. Step 2: Check greet method output

    The greet() method accesses this.name and returns "Hello, Anna!" because the property was set correctly.
  3. Final Answer:

    Hello, Anna! -> Option D
  4. 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

  1. Step 1: Check parameter properties syntax

    The constructor parameter model uses public but lacks a type annotation, which is required in TypeScript.
  2. 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.
  3. Final Answer:

    Missing type annotation for parameter 'model'. -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    class Book { constructor(public title: string, public author: string) {} } -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard