0
0
Typescriptprogramming~20 mins

Removing modifiers with minus in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Modifier Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
ACompilation error
BAlice
CTypeError at runtime
DBob
Attempts:
2 left
💡 Hint
Removing the readonly modifier allows properties to be changed.
Predict Output
intermediate
2: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);
AJohn
BCompilation error: Property 'name' is missing
Cundefined
DTypeError at runtime
Attempts:
2 left
💡 Hint
Removing the optional modifier makes the property required.
🔧 Debug
advanced
2: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);
AMinus modifiers only work on methods, not properties
BCannot remove both readonly and optional modifiers at once
CRemoving optional modifier requires property to be present, but object is empty
DTypeScript does not support mapped types with multiple modifiers
Attempts:
2 left
💡 Hint
Check if the object satisfies the required properties after removing optional.
🧠 Conceptual
advanced
2: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] };
AProperties in U are mutable and required
BProperties in U are readonly and optional
CProperties in U are mutable and optional
DProperties in U are readonly and required
Attempts:
2 left
💡 Hint
Minus removes modifiers from properties.
📝 Syntax
expert
2: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?
Atype A = { -readonly [K in keyof T]: T[K] };
Btype D = { -readonly? [K in keyof T]: T[K] };
Ctype C = { -readonly -? [K in keyof T]: T[K] };
Dtype B = { -? [K in keyof T]: T[K] };
Attempts:
2 left
💡 Hint
Check the correct syntax for removing modifiers with minus.