New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(react): improve split props fn #40
Comments
Great idea! I think this can be achieved with an additional generic. |
You are awesome. Without investigating the topic I think there is a built-in required type in typescript. In addition if it makes the fn easier I don't think we need the grouping right? |
I tried to get the typings right but it is not as simple as it seems - obviously :D My journey was: We need to type the array to ensure every key of an additional type is present in the array. type UseMachineProps = { value: string, onChange: any }
// =>
type Union = 'value' | 'onChange'
// =>
type Tuple = ['value', 'onChange']
// =>
function splitProps(props, selection: UnionToTupleMagic) {} But a Tuple type enforces the order of the props to match exactly type UseMachineProps = { value: string, onChange: any }
type Tuple = ['value', 'onChange'] // works
type Tuple = ['onChange', 'value'] // fails This is not very good DX, because it is hard to get this order correctly for 20 props... I created a TS playground for that: |
Maybe we can get some inspiration here: microsoft/TypeScript#50831 (comment) |
Two ideas that came to mind:
a) when using the
splitProps
fn I would love to define a target type that is used for autocompletionb) when using the
splitProps
fn it should fail on typecheck if not all props are destructured. so if a new property is added our build pipeline would failhere is an example:
Love to hear your thoughts @TimKolberger
The text was updated successfully, but these errors were encountered: