Created
February 1, 2023 10:25
-
-
Save araqnid/dff1c979fbd676cf8d4ca22f72ca00c1 to your computer and use it in GitHub Desktop.
Convert optional fields of a Typescript interface to nullable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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