2

I have a type like this one:

interface A {
  a: string
  b: string | null
}

I would like to generate the same type but each nullable value becomes optional:

interface A {
  a: string
  b?: string | null
}

Something like that but only for nullable values (this one makes all values optional):

export type NullValuesToOptional<T> = {
  [P in keyof T]?: T[P]
}
Paleo
  • 21,831
  • 4
  • 65
  • 76

1 Answers1

3

Extracting the nullable field keys and then generating a new type based on that information would work.
This answer on removing never types holds the key to the puzzle.

interface A {
  a: string
  b: string | null
  c?: string | null;
  d?: string;
}

// Built-in NonNullable also catches undefined
type NonNull<T> = T extends null ? never : T;
type NullableKeys<T> = NonNullable<({
  [K in keyof T]: T[K] extends NonNull<T[K]> ? never : K
})[keyof T]>;

type NullValuesToOptional<T> = Omit<T, NullableKeys<T>> & Partial<Pick<T, NullableKeys<T>>>;

type B = NullValuesToOptional<A>;

Not as straight-forward as I'd have hoped, though.

Paleo
  • 21,831
  • 4
  • 65
  • 76
Etheryte
  • 24,589
  • 11
  • 71
  • 116
  • 2
    Thank a lot for your answer! I edited your code just to add a name to the type. I thought I could transform this code to make it recursive, but I couldn't. I just created a new question for that: https://stackoverflow.com/questions/58411991/apply-a-complex-generic-type-recursively . I'll be grateful if you have the answer. – Paleo Oct 16 '19 at 11:15