TypeScript: type A = {kind: "a"} type B = {kind: "b"} type AorB = A | B function takesB(b: B) { } function makeB(): AorB { return {kind: "b"} } I can do: const b = makeB() b.kind === "b" && takesB(b) But how do I make this work? const isB = b.kind === "b" isB && takesB(b)
-
-
It is! And it says exactly what it means without any fancy language features! Thank you!
-
Any time :)
End of conversation
New conversation -
-
-
is there a reason this works but it doesn't work if you try to put the conditionals inline?
-
(Not an expert in TS, this is just my understanding. Take it with a grain of salt). `const b = aOrB.kind ==="b" && b` assigns a new variable with a known type of `B`. `const isB = b.kind === "b"` assigns a variable of type `boolean` not `b is B`.
- 3 more replies
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.