-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Error when destructing union #6083
Comments
Use generics like this |
It has nothing to do with generics and your example fails |
It does not "fails". Read the error message. I added the error intentionally to show use that it works. |
This was just an example. This error occurs allot when destructing React component's props. Here's another example: type Message = { type: 'image', data: { url: string } } | { type: 'text', data: { text: string } };
// Doesn't work
const MessageBubble = ({ type, data } : Message) => {
switch (type) {
case 'image': {
return <img src={data.url} />;
}
case 'text': {
return <div>{data.text}</div>;
}
default: {
throw new Error('Unknown message type');
}
}
}
// Works
const MessageBubble2 = (props : Message) => {
switch (props.type) {
case 'image': {
return <img src={props.data.url} />;
}
case 'text': {
return <div>{props.data.text}</div>;
}
default: {
throw new Error('Unknown message type');
}
}
} |
Oh, sorry, it seems like a duplicate of #3918 |
No, it says unions doesn't work well with destructions |
I personally can't see any difference between your latest example and an example from #3918 |
Because this has nothing to do with refinements |
Well, what about #3932 ? |
Duplicate of #3932 |
When destructing a union object flow fails
https://flow.org/try/#0C4TwDgpgBAysCuATCA7YUC8UDeAoAkKJAFxQDkAzgsmmbgL665HQAqEAhgMYAWEATphwEWpMsE68BdRri4B7FFSgBzCMFbhoWABTYoLKPSik4SVOgA+Udtz78AlJgB8BrUA
The text was updated successfully, but these errors were encountered: