paldepind / union-type

A small JavaScript library for defining and using union types.
MIT License
477 stars 28 forks source link

With Type.check = false, avoid TypeError #49

Closed foxdonut closed 7 years ago

foxdonut commented 7 years ago

With Type.check = false, avoid TypeError: Cannot read property 'apply' of undefined.

After setting Type.check = false, we should be able to write a handler that handles some, but not all, cases, without needing a catchall _. This saves us from having to write boilerplate _: function() { } code in handlers.

foxdonut commented 7 years ago

I am using union-type for Meiosis examples. Thank you @paldepind for union-type, it's a great fit!

This PR would avoid the need for boilerplate _, for example: https://github.com/foxdonut/meiosis-examples/blob/master/examples/temperatures/date/receive.js#L19

foxdonut commented 7 years ago

@paldepind thank you for looking at my PR. Indeed in the example I showed, it is to perform a side effect. But that is not the only use case. I also have something like var modelUpdate = proposal.case({...}); and if modelUpdate is defined, I'll do something with it. Like you said, when a case isn't handled, I just want modelUpdate to be undefined.

Thanks!

paldepind commented 7 years ago

I see. So you'll check if modelUpdate is undefined later in the code.

Merged and released :smile_cat: Thank you for the PR!

foxdonut commented 7 years ago

Thank you @paldepind , much appreciated!