Last active
June 3, 2024 17:22
-
-
Save bigmistqke/71ac2850fd2211654f34e9e1ae8761e5 to your computer and use it in GitHub Desktop.
`processProps` and `defaultProps` utilities for solid-js
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
import { MergeProps, mergeProps, splitProps } from 'solid-js' | |
type KeyOfOptionals<T> = keyof { | |
[K in keyof T as T extends Record<K, T[K]> ? never : K]: T[K] | |
} | |
export function processProps< | |
TProps extends Record<string, any>, | |
TKey extends KeyOfOptionals<TProps>, | |
TSplit extends (keyof TProps)[], | |
>(props: TProps, defaults: Required<Pick<TProps, TKey>>, split?: TSplit) { | |
return splitProps(defaultProps(props, defaults), split || []) | |
} | |
// from https://github.com/solidjs/solid/issues/1526#issuecomment-1623035790 | |
export function defaultProps<T, K extends KeyOfOptionals<T>>( | |
props: T, | |
defaults: Required<Pick<T, K>>, | |
): MergeProps<[Required<Pick<T, K>>, T]> { | |
return mergeProps(defaults, props) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment