Skip to content

Instantly share code, notes, and snippets.

@araqnid
Created February 1, 2023 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save araqnid/dff1c979fbd676cf8d4ca22f72ca00c1 to your computer and use it in GitHub Desktop.
Save araqnid/dff1c979fbd676cf8d4ca22f72ca00c1 to your computer and use it in GitHub Desktop.
Convert optional fields of a Typescript interface to nullable
type NullableFields<T> = Required<{
[K in keyof T]: {} extends Pick<T, K> ? Exclude<T[K], undefined> | null : T[K]
}>
interface Person {
name: string
age?: number
}
const person: NullableFields<Person> = {
name: 'John',
age: null,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment