0

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.

distante
  • 6,438
  • 6
  • 48
  • 90

1 Answers1

2

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]>
};

:)

distante
  • 6,438
  • 6
  • 48
  • 90