metadevpro/ts-pegjs

docs: exception handler

Closed this issue · 4 comments

I'm not sure this works as intended. I get a TypeScript error from the exception handler. The goal in that handler is being able to call format() with type safety.

import { PeggySyntaxError, parse } from './arithmetics';

try {
    const sampleOutput = parse('my sample...');
} catch (ex: PeggySyntaxError) {
    // Handle parsing error
    // [...]
}

Hi. It looks like the exported of the type in arithmetics.ts is not property exported:

export const PeggySyntaxError = peggyParser.SyntaxError as typeof _PeggySyntaxError;

I this this fix will work:

export type PeggySyntaxError = _PeggySyntaxError;

Can you confirm if this @hildjj ? cc @siefkenj

Ah. This is very complicated, it seems, but I think I found a solution.

The issue is PeggySyntaxError is not just a type, it's also a class that can be constructed.

@pjmolina Your fix is correct, but it needs to be exported both ways. That is, the generated code should be

export const PeggySyntaxError = peggyParser.SyntaxError as typeof _PeggySyntaxError;
export type PeggySyntaxError = _PeggySyntaxError;

so that the constructor and the type can both be exported.

I think DefaultTracer will also have the same issue since it is exported the same way as PeggySyntaxError.

Indeed, good catch @siefkenj
Let me fix the PR.

I think PR #103 is ready to fix this.