Advanced TypeScript Generics Quiz Quiz

Explore complex TypeScript generics concepts with questions on conditional types, generic constraints, mapped types, and advanced utility patterns. This quiz is designed to deepen understanding of sophisticated TypeScript generic features and their practical use.

  1. Conditional Types in Generics

    Which output best describes the result of the following conditional generic type: type Resultu003CTu003E = T extends string ? number : boolean, when T is assigned 'string'?

    1. boolean
    2. number
    3. void
    4. string

    Explanation: When T is assigned 'string', the type Resultu003CTu003E resolves to number because the condition T extends string is true. The option 'boolean' would be selected if T did not extend string. 'string' is incorrect as it is the input type, not the result of the conditional. 'void' is unrelated as it is not specified in the type definition.

  2. Generic Constraints

    Given function identityu003CT extends { id: number }u003E(item: T): number { return item.id }, which will cause a TypeScript error if passed as an argument?

    1. { id: 12, name: 'test' }
    2. { id: 42 }
    3. { id: 0 }
    4. { id: '42' }

    Explanation: The constraint T extends { id: number } ensures that the argument must have a numeric 'id' property. The object { id: '42' } has 'id' as a string, violating the constraint. The other options either have the correct type for 'id' or additional properties which do not break the constraint. '42' as a string fails the number requirement, which is why it’s the only incorrect choice.

  3. Mapped Types and Transformation

    Given type Flagsu003CTu003E = { [K in keyof T]: boolean }, what is the resulting type for Flagsu003C{ a: number, b: string }u003E?

    1. { a: boolean; }
    2. { a: boolean; b: string; }
    3. { a: number; b: string; }
    4. { a: boolean; b: boolean; }

    Explanation: The mapped type Flagsu003C{ a: number, b: string }u003E transforms each property value type to boolean, so both 'a' and 'b' become booleans. The second option doesn't change the types, which is incorrect. The third option only changes 'a', not 'b'. The fourth option omits 'b', failing to represent all keys from the original object.

  4. Default Type Parameters

    For the declaration type Boxu003CT = stringu003E = { value: T }, what will Boxu003Cu003E resolve to when used without specifying a generic type?

    1. { value: number }
    2. { value: T }
    3. { value: string }
    4. { value: boolean }

    Explanation: When the generic type parameter T is omitted, Boxu003Cu003E uses the default, which is string, so the resulting type is { value: string }. { value: number } and { value: boolean } are incorrect because T does not default to those types. { value: T } is too generic and does not reflect the resolved type.

  5. Inferring Types with 'infer' Keyword

    Consider the type definition: type ElementTypeu003CTu003E = T extends (infer U)[] ? U : T. What type will ElementTypeu003Cnumber[]u003E evaluate to?

    1. number[]
    2. number
    3. any[]
    4. U

    Explanation: With T as number[], the type extends (infer U)[] matches, so U is inferred as number, making ElementTypeu003Cnumber[]u003E equal to number. any[] and number[] are both array types, while the correct output should be the element type of the array. 'U' is just a generic placeholder and does not represent a concrete type.