import { IParser, OnParse } from "./interfaces";
export class GuardParser implements IParser {
constructor(
readonly checkIsA: (value: A) => value is A & B,
readonly typeName: string,
readonly description = {
name: "Guard",
children: [],
extras: [typeName],
} as const,
) {}
parse(a: A, onParse: OnParse): C | D {
if (this.checkIsA(a)) {
return onParse.parsed(a);
}
return onParse.invalid({
value: a,
keys: [],
parser: this,
});
}
}