Skip to content

Commit

Permalink
Even more compile time checking
Browse files Browse the repository at this point in the history
  • Loading branch information
markw65 committed Sep 9, 2023
1 parent f6e2e3e commit 1f6eb83
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions test/types/peg.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,17 @@ describe("peg.d.ts", () => {
},
});

// Extract the visitor object
type VisitorArg
= typeof visit extends peggy.compiler.visitor.Visitor<infer U>
? U : never;

// Extract the functions that don't return `any`
type DefinedKeys = keyof {
[K in keyof VisitorArg as VisitorArg[K] extends (...args: any) => any
? unknown extends ReturnType<VisitorArg[K]> ? never : K : never]: true
};

visit(grammar);

const astKeys = [
Expand All @@ -450,12 +461,11 @@ describe("peg.d.ts", () => {
"text",
"top_level_initializer",
"zero_or_more",
] as const;
] satisfies AstTypes[];

expect(Object.keys(visited).sort()).toStrictEqual(astKeys);
for (const key of astKeys) {
expectType<AstTypes>(key);
}
expectType<AstTypes[]>(astKeys);
expectType<DefinedKeys[]>(astKeys);
});

it("compiles", () => {
Expand Down

0 comments on commit 1f6eb83

Please sign in to comment.