Closed mooncaker816 closed 1 year ago
Do you have some example code that doesn't type check correctly?
Yes, this is a simple example shows that the type guard works differently when the VisitInfo
is provided or not.
https://codesandbox.io/s/typescript-playground-export-forked-0qlgow?file=/index.ts
Basically i want to extract the group contents under some specific macro (e.g \abc{{1}{2}{3}{4}}
)
with VisitInfo
:
const groupMatcher1 = (node: Ast.Ast, info: VisitInfo): node is Ast.Group =>
match.group(node) && match.createMacroMatcher(["abc"])(info.parents[1]);
const extractGroups1 = (root: Ast.Root): string[] => {
let groups: string[] = [];
visit(
root,
(group) => {
// group is typed as "Ast.Node | Ast.Argument" not "Ast.Group"
groups.push(printRaw(group.content));
},
{
test: groupMatcher1
}
);
return groups;
};
without VisitInfo
const extractGroups2 = (root: Ast.Root): string[] => {
let groups: string[] = [];
visit(
root,
(group) => {
// group is typed as "Ast.Group"
groups.push(printRaw(group.content));
},
{
test: match.group
}
);
return groups;
};
Thanks! PR #31 should fix this and hopefully be robust to any future changes.
Thank you for this great lib.
Should be fixed in 1.3.2 :-D
It looks like the signature of test function is not consistent.
there is a second argument
info
in the test function ofVisitOptions
:but currently only one argument is in the signature of the test function constraint, so
PossibleTypes
will always be inferred as the type of node when the test function has two arguments.Maybe the signature should be changed to
(node: Ast.Ast, info?: VisitInfo)
and(x: any, y?: any)
?