0
0
Typescriptprogramming~20 mins

Property decorators in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Property Decorator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this TypeScript code using a property decorator?

Consider the following TypeScript code with a property decorator that logs when a property is accessed:

function logProperty(target: any, key: string) {
  let value = target[key];
  const getter = () => {
    console.log(`Get: ${key} => ${value}`);
    return value;
  };
  const setter = (newVal: any) => {
    console.log(`Set: ${key} => ${newVal}`);
    value = newVal;
  };
  Object.defineProperty(target, key, {
    get: getter,
    set: setter,
    enumerable: true,
    configurable: true
  });
}

class Person {
  @logProperty
  name: string = "Alice";
}

const p = new Person();
p.name = "Bob";
console.log(p.name);

What will be printed to the console?

Typescript
function logProperty(target: any, key: string) {
  let value = target[key];
  const getter = () => {
    console.log(`Get: ${key} => ${value}`);
    return value;
  };
  const setter = (newVal: any) => {
    console.log(`Set: ${key} => ${newVal}`);
    value = newVal;
  };
  Object.defineProperty(target, key, {
    get: getter,
    set: setter,
    enumerable: true,
    configurable: true
  });
}

class Person {
  @logProperty
  name: string = "Alice";
}

const p = new Person();
p.name = "Bob";
console.log(p.name);
A
Set: name => Bob
Get: name => Bob
Bob
B
Set: name => Alice
Set: name => Bob
Get: name => Bob
Bob
C
Get: name => Alice
Set: name => Bob
Get: name => Bob
Bob
D
Set: name => Bob
Bob
Attempts:
2 left
💡 Hint

Think about when the setter and getter functions are called during property access and assignment.

🧠 Conceptual
intermediate
1:30remaining
What does a property decorator receive as arguments in TypeScript?

In TypeScript, when you create a property decorator, what are the arguments passed to the decorator function?

AThe target object and the property name as a string or symbol
BThe target object, property name, and property descriptor
COnly the property descriptor
DThe target object, property name, and the initial property value
Attempts:
2 left
💡 Hint

Property decorators do not receive a property descriptor argument.

🔧 Debug
advanced
2:30remaining
Why does this property decorator not log property access as expected?

Look at this TypeScript code with a property decorator intended to log access to a property:

function logAccess(target: any, key: string) {
  let value = target[key];
  Object.defineProperty(target, key, {
    get() {
      console.log(`Getting ${key}`);
      return value;
    },
    set(newVal) {
      console.log(`Setting ${key} to ${newVal}`);
      value = newVal;
    },
    enumerable: true,
    configurable: true
  });
}

class Example {
  @logAccess
  data = 123;
}

const e = new Example();
console.log(e.data);
e.data = 456;
console.log(e.data);

However, when running this code, no logs appear when accessing or setting data. Why?

ABecause the decorator is missing a return statement to replace the property descriptor
BBecause the decorator modifies the prototype, but the instance property shadows it with its own value
CBecause property decorators cannot use Object.defineProperty
DBecause the property is static and the decorator targets instance properties
Attempts:
2 left
💡 Hint

Think about how instance properties are initialized versus prototype properties.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a property decorator that adds a default value?

Which of the following TypeScript property decorators correctly sets a default value 42 for a property?

A
function defaultValue(target: any, key: string) {
  Object.defineProperty(target, key, {
    get() { return 42; },
    set(value) { }
  });
}
B
function defaultValue(target: any, key: string) {
  target[key] = 42;
}
C
function defaultValue(target: any, key: string) {
  return {
    value: 42,
    writable: true
  };
}
D
function defaultValue(target: any, key: string) {
  Object.defineProperty(target, key, {
    value: 42,
    writable: true,
    configurable: true
  });
}
Attempts:
2 left
💡 Hint

Remember that property decorators do not receive a descriptor and cannot return one. Use Object.defineProperty properly.

🚀 Application
expert
2:30remaining
How many properties will the object have after applying this property decorator?

Given this TypeScript code with a property decorator that replaces the property with a getter and setter storing the value in a private symbol:

const _value = Symbol('value');

function storeValue(target: any, key: string) {
  Object.defineProperty(target, key, {
    get() {
      return this[_value];
    },
    set(val) {
      this[_value] = val;
    },
    enumerable: true,
    configurable: true
  });
}

class Data {
  @storeValue
  prop = 10;
}

const d = new Data();
d.prop = 20;

After running this code, how many own properties does the object d have?

A1
B3
C0
D2
Attempts:
2 left
💡 Hint

Consider where the symbol property is stored and whether it is enumerable.