Cannot use type guard functions created by scanner() in union().
yona3 opened this issue · 1 comments
yona3 commented
If you pass a type guard function created by scanner()
as an argument of union()
as follows, it will throw an error and will not work properly.
This is because scanner()
is designed to throw an exception instead of returning false
usually.
type Bar = {
a: string;
b: Foo | null;
c: string | null;
};
const bar = {
a: "a",
b: null,
c: null,
};
const isBar = scanner<Bar>({
a: string,
b: union(isFoo, Null), // throw error!
c: union(string, Null),
});
yona3 commented
This problem can be solved by setting the isUseWithUnion
option to true
for scanner
used in union()
like this. This is available in v0.4.0
.
const isFoo = scanner<Foo>(
{
a: string,
b: number,
c: boolean,
d: date,
e: array(string),
f: optional(string),
g: list(["a", "b", "c"]),
h: union(string, Null),
i: union<string | number>(string, number),
},
{ isUseWithUnion: true }
);