typescripttypestuplesvariadic-tuple-types

How to convert a tuple object literal type and make all properties of the object literal type required


I have an tuple type that contains multiple object literal types, lets call it Actions for this example. Some of the object literal types contains optional properties. I would like to have a type that makes all properties in the object literal of the tuple type required.

type ClickEvent = {
  type: 'click-event';
  name: string;
  value1?: string;
};

type PerformSearch = {
  type: 'perform-search';
  referrerPage?: string;
};

type Actions = [ClickEvent, PerformSearch]; // only two items to keep it simple for this example


type RequireAllObjectLiterals<T extends unknown[]> = ??? // how does this look like?

type RequiredActions = RequireAllObjectLiterals<Actions>;
//type should look like these at the end -> [Required<ClickEvent>, Required<PerformSearch>]


Solution

  • Mapped types on array/tuple types produce array/types, and the type parameter you're indexing over can be interpreted as the numberlike keys of the array/tuple type. That is, if you write type Mapping<T> = {[I in keyof T]: F<I>}, then assuming T is, say, a three element tuple type [X,Y,Z], then the type parameter I iterates over "0", "1", and "2" and a new tuple type is produced like [F<"0">, F<"1">, F<"2">].

    So the solution here is as straightforward as:

    type RequireAllObjectLiterals<T extends unknown[]> =
        { [I in keyof T]: Required<T[I]> }
    

    And you can verify that it behaves as desired:

    type Actions = [ClickEvent, PerformSearch];
    type RequiredActions = RequireAllObjectLiterals<Actions>;
    // type RequiredActions = [Required<ClickEvent>, Required<PerformSearch>]
    

    Playground link to code