'How to map tuples with value | undefined to just value
I am trying to create a type that map a tuple like this:
[number, string | undefined, number | undefined, string | null]
to
[number, string , number, string]
But I am not able to do it.
I tried something like this but obviously does not work
type FilterUndefined<T extends readonly unknown[]> = T extends readonly [infer H, ...infer R] ?
H extends undefined ? null : [H, ...FilterUndefined<R>] : T
This question sounds similar, but it extracts from an object.
Any help is appreciated.
Solution 1:[1]
Oh, this was easier that I thought. I forgot about the NonNullable helper.
type NotNullableTuple<Tuple extends readonly unknown[]> = {
[Index in keyof Tuple]: NonNullable<Tuple[Index]>
};
:)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | distante |
