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)
Does this fit your needs? function isB(aOrB: AorB): aOrB is B { return aOrB.kind === "b" } isB(b) && takesB(b);
-
-
It will help. My goal here is to avoid breaking JSX lines. I have: {chooseable.kind === "lesson"&& ... You propose this, which is shorter: isLesson(chooseable) && ... But what I really want is: isLesson && ...
-
Yeah, I don't think it's possible to get exactly that (since once it's assigned it's a bool and no longer a type guard). This is one other option though? const definitelyB = b.kind === "b" && b; definitelyB && takesB(definitelyB);
- 2 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.