Created
June 1, 2020 12:42
-
-
Save zaverden/96c77a53d795d2890141dcf12919e651 to your computer and use it in GitHub Desktop.
withOptional
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 NullPartial<T> = { | |
| [P in keyof T]?: T[P] | null; | |
| }; | |
| type Optional<T, K extends keyof T> = Omit<T, K> & NullPartial<T>; | |
| function withOptional<TProps, TOpts extends Array<keyof TProps>>( | |
| Wrapped: ComponentType<TProps>, | |
| optionals: TOpts, | |
| Placeholder: ComponentType<Optional<TProps, TOpts[number]>>, | |
| ): ComponentType<Optional<TProps, TOpts[number]>> { | |
| return props => { | |
| if (optionals.some(f => props[f] == null)) { | |
| return <Placeholder {...props} />; | |
| } | |
| return <Wrapped {...(props as TProps)} />; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment