Challenge - 5 Problems
Modifier Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of removing readonly modifier with minus
What is the type of
MutablePerson after removing the readonly modifier from Person?Typescript
type Person = {
readonly name: string;
readonly age: number;
};
type MutablePerson = {
-readonly [K in keyof Person]: Person[K];
};
const p: MutablePerson = { name: "Alice", age: 30 };
p.name = "Bob";
console.log(p.name);Attempts:
2 left
💡 Hint
Removing the readonly modifier allows properties to be changed.
✗ Incorrect
The minus sign before readonly removes the readonly modifier, so the property 'name' can be reassigned. The console.log prints the updated value 'Bob'.
❓ Predict Output
intermediate2:00remaining
Effect of removing optional modifier with minus
What is the type of
RequiredUser after removing the optional modifier from User?Typescript
type User = {
id: number;
name?: string;
};
type RequiredUser = {
-? [K in keyof User]: User[K];
};
const user: RequiredUser = { id: 1, name: "John" };
console.log(user.name);Attempts:
2 left
💡 Hint
Removing the optional modifier makes the property required.
✗ Incorrect
The minus sign before ? removes the optional modifier, so 'name' must be present. The code assigns 'John' and logs it.
🔧 Debug
advanced2:00remaining
Why does this code cause a compilation error?
Consider this code snippet using minus modifiers. Which option explains the cause of the error?
Typescript
type Flags = {
readonly active?: boolean;
};
type MutableFlags = {
-readonly -? [K in keyof Flags]: Flags[K];
};
const flags: MutableFlags = {};
console.log(flags.active);Attempts:
2 left
💡 Hint
Check if the object satisfies the required properties after removing optional.
✗ Incorrect
Removing the optional modifier makes 'active' required, but the object is empty, causing a compilation error.
🧠 Conceptual
advanced2:00remaining
Understanding combined minus modifiers in mapped types
What is the effect of the following mapped type?
type T = { readonly a?: number };
type U = { -readonly -? [K in keyof T]: T[K] };Attempts:
2 left
💡 Hint
Minus removes modifiers from properties.
✗ Incorrect
The minus signs remove readonly and optional modifiers, so properties become mutable and required.
📝 Syntax
expert2:00remaining
Which option causes a syntax error when removing modifiers?
Which of the following mapped type definitions is syntactically invalid in TypeScript when trying to remove modifiers?
Attempts:
2 left
💡 Hint
Check the correct syntax for removing modifiers with minus.
✗ Incorrect
Option B is invalid because '-readonly?' is not valid syntax; modifiers must be removed separately with '-' before each modifier.