Discriminating PS sum type in JS code


i have some code that used to work for doing this (albeit that i always meant to change it as the TODO comment shows):

// string conversions should be done on the Purescript side for type safety and totality reasons TODO
  if(thing instanceof PS["Module Name"].A) {
    // do something typically heinous in JS

but now i’m using Webpack and while i can require in the module i never get my instanceOf to work - even when i can plainly see that the thing is an A

Is instanceOf even the right way to be doing this? should i just knuckle down and do the conversion from a PS type to stringly-typed thing on the PS side?

any thoughts, comments appreciated…


Hope this helps.


Thanks a lot for responding. It helps to know that there’s another way to do it.

I don’t understand why the you say the instanceof way “is definitely cumbersome”? It seems to require less code than the solution you outline…especially for big sum types like flag sets or options

Maybe the use case you guys had involved giving this code or coding pattern over to JavaScript-only developers?

in any case, thanks again for the response


One advantage to writing folds like @paulyoung illustrated is that your JS code doesn’t have to make assumptions about how your PS data type is represented, and it’s easier to maintain compatibility (it’s not type checked after all). I’m personally not a huge fan of writing code that depends on particulars around PS compiler representations.