Created
March 11, 2025 22:29
-
-
Save vjau/0d3b1b72bb4ee632b18a770c18870ad4 to your computer and use it in GitHub Desktop.
Typescript utility types
This file contains hidden or 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 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