Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript: meriyah package is not compatible with estree package #199

Open
fis-cz opened this issue Dec 10, 2021 · 1 comment
Open

TypeScript: meriyah package is not compatible with estree package #199

fis-cz opened this issue Dec 10, 2021 · 1 comment

Comments

@fis-cz
Copy link

fis-cz commented Dec 10, 2021

Hello,

Just trying to use 'meriyah' (4.2.0) with 'estree-toolkit' (1.2.6) and '@types/estree' (0.0.50), so latest available versions, with TypeScript compiler. I just found that Meriyah NPM package is not compatible with '@types/estree' NPM package.

When I do:

import { Program } from "estree";
import { parseModule } from "meriyah";

const ast: Program = parseModule("");

the type checker reports the error:

Type 'import(".../node_modules/meriyah/dist/src/estree").Program' is not assignable to type 'import(".../node_modules/@types/estree/index").Program'.
  Types of property 'body' are incompatible.
    Type 'Statement[]' is not assignable to type '(Directive | Statement | ModuleDeclaration)[]'.
      Type 'Statement' is not assignable to type 'Directive | Statement | ModuleDeclaration'.
        Type 'BlockStatement' is not assignable to type 'Directive | Statement | ModuleDeclaration'.
          Type 'import(".../meriyah/dist/src/estree").BlockStatement' is not assignable to type 'import(".../node_modules/@types/estree/index").BlockStatement'.
            Types of property 'body' are incompatible.
              Type 'import(".../node_modules/meriyah/dist/src/estree").Statement[]' is not assignable to type 'import(".../node_modules/@types/estree/index").Statement[]'.
                Type 'import(".../node_modules/meriyah/dist/src/estree").Statement' is not assignable to type 'import(".../node_modules/@types/estree/index").Statement'.

So basically, the Program type returned by parseModule is not inferred from Program declared in the @types/estree.

Although I can go around it (and it seems working correctly in runtime) by:

import { Program } from "estree";
import { parseModule } from "meriyah";

const ast: Program = <any>parseModule("");

It would be great if this works out of the box. Is it possible to use same types?

@3cp
Copy link
Member

3cp commented Mar 18, 2022

I am also using forced type conversion to tap into various estree tools.

import {parseScript, Options} from 'meriyah';
import * as ESTree from 'estree';

const paserOptions: Options = {
  next: true,
  ranges: true,
  loc: true,
  webcompat: true,
  globalReturn: true,
  impliedStrict: true
};

export default function (code: string): ESTree.Program {
  return parseScript(code, paserOptions) as ESTree.Program;
}

It's possible to refactor meriyah code to use estree types (that will introduce a dependency which we don't like), but needs quite some work.

The types defined in meriyah didn't strictly follow estree, especially the inheritance of estree types. The generated data structure conforms to estree, but our type definitions might have some differences (I cannot recall some details). Sorry, I (the only active maintainer right now) don't have plan to work on this in near future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants