Consider this TypeScript code using Partial and Required utility types. What will be the output when running the code?
type Person = {
name: string;
age?: number;
};
const partialPerson: Partial<Person> = { name: "Alice" };
const requiredPerson: Required<Person> = { name: "Bob", age: 30 };
console.log(partialPerson.age);
console.log(requiredPerson.age);Think about what Partial and Required do to the properties of a type.
Partial makes all properties optional, so age can be missing and is undefined. Required makes all properties required, so age must be present and is 30.
Pick utility type?Which of the following best explains why the Pick utility type is useful in TypeScript?
Think about how you can create smaller types from bigger ones.
Pick lets you create a new type by choosing only some properties from another type, which helps keep code focused and safe.
Look at this code using Record utility type. What error will TypeScript show?
type Roles = 'admin' | 'user'; const rolePermissions: Record<Roles, string[]> = { admin: ['read', 'write'], user: ['read'], guest: ['read'] };
Check if all keys in the object match the keys in the Record type.
Record requires exactly the keys specified. 'guest' is not part of 'Roles', so TypeScript errors.
Exclude utility type?Given type T = 'a' | 'b' | 'c', which option correctly excludes 'b' from T?
Remember the order of parameters in Exclude: first is the union, second is what to remove.
Exclude removes 'b' from T, leaving 'a' | 'c'. Other options either reverse parameters or exclude wrong values.
Given this code, how many properties does the type Flags have?
type Keys = 'featureA' | 'featureB' | 'featureC'; type Flags = { [K in Keys as K extends 'featureB' ? never : K]: boolean; };
Look at the conditional key remapping in the mapped type.
The mapped type excludes 'featureB' by mapping it to never, so only 'featureA' and 'featureC' remain, totaling 2 properties.