Skip to content
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

Closed
cschroeter opened this issue Nov 1, 2022 · 5 comments
Closed

feat(react): improve split props fn #40

cschroeter opened this issue Nov 1, 2022 · 5 comments
Assignees

Comments

@cschroeter
Copy link
Member

Two ideas that came to mind:

a) when using the splitProps fn I would love to define a target type that is used for autocompletion
b) 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 fail

here is an example:

interface Maschine {
  value?: string
  newProperty?: string // was added recently
}

const [machineProps, rest] = splitProps<Maschine>(props, ["value", "newProperty"]) // OK
const [machineProps, rest] = splitProps<Maschine>(props, ["value"]) // Error -> missing value newProperty

Love to hear your thoughts @TimKolberger

@TimKolberger
Copy link
Contributor

Great idea! I think this can be achieved with an additional generic.
I'll give it a shot tomorrow.

@TimKolberger TimKolberger self-assigned this Nov 1, 2022
@cschroeter
Copy link
Member Author

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?

@TimKolberger
Copy link
Contributor

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.
Convert the keyof TargetProps union to a tuple, and integrate it into the splitProps type defs.

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...
Do you have any ideas how to finalize this?

I created a TS playground for that:

https://www.typescriptlang.org/play?#code/C4TwDgpgBACgTgezAZQgGwgY2ASwQOwB54kBnAPigF4o4IBDAEwLRCgAoBrCEBAM1i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O77jgmD7smEz0Js9BA8Af0RBAtSCRE+6qTNcFzRoSULWlS0rchpqodthXYTtpB7VVS1-lZIhAA

@TimKolberger
Copy link
Contributor

Maybe we can get some inspiration here: microsoft/TypeScript#50831 (comment)

@cschroeter
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants