cassiozen / useStateMachine

The <1 kb state machine hook for React
MIT License
2.38k stars 47 forks source link

DX for typed events #65

Closed cassiozen closed 3 years ago

cassiozen commented 3 years ago

Hey @devanshj, when using types events, I'm getting a confusing error message. Here's a sample case:

1 - I have a types "UPDATE" event that requires a string value 2 - When trying send({ type: "UPDATE" }) it gives me an error (right, it is missing the value) 3 - Problem is, the error says Type '"UPDATE"' is not assignable to type '"some other type"':

Here is an example (the examples/form with this added send on line 57:

Screen Shot 2021-07-28 at 9 49 43 AM
cassiozen commented 3 years ago

I understand that TypeScript is showing one example of a correct possible value - but since you apparently can bend TypeScript to your will 🤪, can anything be done here?

devanshj commented 3 years ago

you apparently can bend TypeScript to your will 🤪

HAHAHAHA this is 100% going to twitter xD

Could you send that code? I can't repro minimally. EDIT: oh wait you said it's from examples, looking into it

devanshj commented 3 years ago

This is most probably a bug, so i'll report it to typescript team, meanwhile let me see if I can find a workaround

devanshj commented 3 years ago

The repro is simpler than I thought xD I was guessing it takes time to compute hence typescript was showing bad errors

image

And the workaround is pretty simple - to add an overload, sending a PR

cassiozen commented 3 years ago

you apparently can bend TypeScript to your will

Stands true ;)

cassiozen commented 3 years ago

sending a PR

I'll publish the beta after we merge this then.

devanshj commented 3 years ago

For reference and reminder to revert overloads when #45217 gets fixed.