0
0
Typescriptprogramming~20 mins

Why utility types are needed in Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Utility Types 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 Partial utility type?
Consider this TypeScript code snippet using the Partial utility type. What will be the output when running the compiled JavaScript?
Typescript
interface User {
  name: string;
  age: number;
}

function updateUser(user: User, fieldsToUpdate: Partial<User>): User {
  return { ...user, ...fieldsToUpdate };
}

const user1: User = { name: "Alice", age: 30 };
const updatedUser = updateUser(user1, { age: 31 });
console.log(updatedUser);
A{"name":"Alice","age":31}
B{"name":"Alice"}
CTypeError at runtime
D{"age":31}
Attempts:
2 left
💡 Hint
Partial allows updating only some properties of an object.
🧠 Conceptual
intermediate
1:30remaining
Why are utility types like Readonly useful in TypeScript?
Which of the following best explains why the Readonly utility type is needed in TypeScript?
ATo make all properties of an object immutable, preventing accidental changes.
BTo allow properties to be deleted from an object.
CTo convert all properties to optional so they can be omitted.
DTo automatically convert all properties to strings.
Attempts:
2 left
💡 Hint
Think about protecting data from being changed after creation.
Predict Output
advanced
2:00remaining
What is the output of this TypeScript code using Record utility type?
Given this TypeScript code using the Record utility type, what will be printed to the console?
Typescript
type Roles = "admin" | "user" | "guest";

const rolePermissions: Record<Roles, string[]> = {
  admin: ["read", "write", "delete"],
  user: ["read", "write"],
  guest: ["read"]
};

console.log(rolePermissions.user.includes("delete"));
ATypeError at runtime
Btrue
Cundefined
Dfalse
Attempts:
2 left
💡 Hint
Check if the 'user' role has 'delete' permission in the array.
🔧 Debug
advanced
1:30remaining
What error does this TypeScript code produce?
This code tries to use the Required utility type. What error will it cause?
Typescript
interface Config {
  url?: string;
  port?: number;
}

const config: Required<Config> = {
  url: "http://example.com"
};
ASyntaxError due to missing comma.
BProperty 'port' is missing in type '{ url: string; }' but required in type 'Required<Config>'.
CTypeError at runtime because 'port' is undefined.
DNo error, code compiles fine.
Attempts:
2 left
💡 Hint
Required makes all properties mandatory.
🚀 Application
expert
2:00remaining
How many keys are in the resulting object after using Pick utility type?
Given this TypeScript code, how many keys does the object 'picked' have?
Typescript
interface Person {
  name: string;
  age: number;
  location: string;
  email: string;
}

const person: Person = {
  name: "Bob",
  age: 25,
  location: "NY",
  email: "bob@example.com"
};

const picked: Pick<Person, "name" | "email"> = {
  name: person.name,
  email: person.email
};
A4
B3
C2
D1
Attempts:
2 left
💡 Hint
Pick selects only specified keys from a type.