TypeScript Utility Types (Partial, Pick, Omit) Quiz Quiz

Challenge your understanding of TypeScript utility types such as Partial, Pick, and Omit. This quiz targets both syntax and practical application to help reinforce your skills with these essential TypeScript features.

  1. Partial Utility Type Usage

    Which option demonstrates the correct way to use the Partial utility type to make every property of the User interface optional?

    1. Partialu003CUseru003E
    2. Optionalu003CUseru003E
    3. Partialu003CUser[]u003E
    4. Useru003CPartialu003E

    Explanation: Partialu003CUseru003E is correct because it constructs a type with all properties of User set as optional. Useru003CPartialu003E is invalid since User is not generic. Optionalu003CUseru003E is not a built-in TypeScript utility type. Partialu003CUser[]u003E would make an array of User objects optional, not the properties within User.

  2. Pick Utility Type Scenario

    If you want a new type that only includes the 'name' and 'email' fields from an existing Employee interface, which utility type should you use?

    1. Selectu003CEmployee, ['name', 'email']u003E
    2. Partialu003CEmployee, 'name', 'email'u003E
    3. Omitu003CEmployee, 'name' | 'email'u003E
    4. Picku003CEmployee, 'name' | 'email'u003E

    Explanation: Picku003CEmployee, 'name' | 'email'u003E is correct because Pick allows you to specify exactly which fields to include in the new type. Omit would do the opposite by excluding specified fields. Partial does not let you select specific keys. Select is not a standard TypeScript utility type.

  3. Omit Utility Type Application

    You wish to exclude the 'password' and 'createdAt' properties from a User type when exposing user details publicly. Which statement correctly applies the Omit utility?

    1. Omitu003CUser, ['password', 'createdAt']u003E
    2. Omitu003CUser, 'password' | 'createdAt'u003E
    3. Removeu003CUser, 'password', 'createdAt'u003E
    4. Picku003CUser, 'password' | 'createdAt'u003E

    Explanation: Omitu003CUser, 'password' | 'createdAt'u003E is the right choice, as it creates a type excluding those specified properties. Pick would select only those properties, which is the opposite of the goal. The array syntax in the third option is invalid for Omit. Remove is not a built-in utility type in TypeScript.

  4. Partial vs Omit

    Given a Product interface, how does Partialu003CProductu003E differ from Omitu003CProduct, 'price'u003E?

    1. Omit makes all fields optional; Partial removes one field
    2. Partial makes all fields optional; Omit removes the specified field
    3. Both remove fields from the type
    4. Both make only specified fields optional

    Explanation: Partialu003CProductu003E creates a version of Product where every property is optional, while Omitu003CProduct, 'price'u003E creates a type that has all Product properties except 'price'. The second and fourth options are incorrect, as only Omit removes fields and neither makes just selected fields optional. The third option reverses the actual behaviors.

  5. Combining Utility Types

    How can you create a type from Order that has all properties optional except for 'id', which must remain required?

    1. Omitu003COrder, 'id'u003E u0026 Requiredu003COrder, 'id'u003E
    2. Partialu003COrderu003E
    3. Picku003COrder, 'id'u003E u0026 Partialu003COmitu003COrder, 'id'u003Eu003E
    4. Partialu003COrderu003E u0026 Omitu003COrder, 'id'u003E

    Explanation: Picku003COrder, 'id'u003E u0026 Partialu003COmitu003COrder, 'id'u003Eu003E results in a type where 'id' is required, and all other fields from Order are optional, thanks to combining Pick and Partial with Omit. Partialu003COrderu003E would make every property optional, including 'id'. Omitu003COrder, 'id'u003E u0026 Requiredu003COrder, 'id'u003E is not valid syntax and does not have the intended behavior. Partialu003COrderu003E u0026 Omitu003COrder, 'id'u003E would simply omit 'id' entirely.