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>]
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 assumingT
is, say, a three element tuple type[X,Y,Z]
, then the type parameterI
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:
And you can verify that it behaves as desired:
Playground link to code