Support for type guards #1565
Answered
by
DenisFrezzato
florianbepunkt
asked this question in
Q&A
-
Not sure if this is an issue or by design. How do type guards work with fp-ts? Given the interfaces A & B interface A {
type: string;
amount: number;
}
interface B extends A {
type: "payout";
amount: number;
} and a type guard const isB = (t: A): t is B => t.type === "payout"; In the following function import { fold } from "fp-ts/boolean";
const example = (t: A) => {
pipe(
t,
isB,
fold(
() => doSomethingWithA(t),
() => doSomethingWithB(t) // t is not typed as B
)
);
}; |
Beta Was this translation helpful? Give feedback.
Answered by
DenisFrezzato
Aug 24, 2021
Replies: 2 comments 1 reply
-
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
florianbepunkt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fold
fromboolean
module works with booleans, so the type level information of a type guard is lost. What you're doing is pattern matching with a variable, so I suggest you to use libraries likets-pattern
orts-adt
.