i'm curious if anyone has explored generating TS u...
# javascript
p
i'm curious if anyone has explored generating TS union type definitions for sealed interfaces/classes?
👀 1
e
If we could, what would the concrete benefit be?
p
you can
switch
on union types in TS and have exhaustive checking, similar to a
when
in kotlin
e
Seems like a good idea then!
How would the type discriminator work with a switch?
you'd probably want to add a literal field with the name of the subtype (or some other identifying field) to switch on
✔️ 1
e
Did check that, was mainly wondering how you'd imagine Kotlin's output. But yeah it seems you have the same idea I was thinking about, adding a literal. But it doesn't feel entirely right to me. Maybe with a series of
instanceof
it's good enough. The outputted JS code should set up the prototype chain correctly.
p
oh like don't even use TS unions and write helper functions?
Maybe with a series of instanceof it's good enough.
i don't think i follow here
e
Can't we use
instanceof
to understand which class we're dealing with?
p
that's not exhaustive
e
Not even when the type is an union?
Never tried it, that's why I'm asking.
p
i don't think it can be, instanceof is only testing a single type
e
TS seems to be aware we covered all the possible cases in a series of
if-elseif
statements with
instanceof
. But the literal discriminator is certainly better.
p
is that only if you're returning / assigning a value?
i'm able to get TS to compile with only one instanceof case
a