Skip to content

Instantly share code, notes, and snippets.

@vjau
Created March 11, 2025 22:29
Show Gist options
  • Save vjau/0d3b1b72bb4ee632b18a770c18870ad4 to your computer and use it in GitHub Desktop.
Save vjau/0d3b1b72bb4ee632b18a770c18870ad4 to your computer and use it in GitHub Desktop.
Typescript utility types
type NullToOptional<K> = {
[key in keyof K as null extends K[key] ? key : never]?: Exclude<K[key], null>
} & {
[key in keyof K as null extends K[key] ? never: key]: K[key]
}
type OptionalToNullable<T> = {
[K in keyof T as undefined extends T[K] ? K : never]-?: T[K] | null; // Convert optional properties to mandatory nullable
} & {
[K in keyof T as undefined extends T[K] ? never : K]: T[K]; // Keep non-optional properties unchanged
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment