javascripttypescriptbabeljsbabel-parser

babel parser cannot parse `import type` declaration


When I try to use parser.parse("import type {Element} from 'react-devtools-shared/src/frontend/types';", {sourceType: "unambiguous"}); to parse the statement, I encounter an error Unexpected token, expected "from".

Is there any problem?

Someone can help me? Thanks a lot.

I try to set errorRecovery:true and strictMode:false, but these are not helpful.


Solution

  • You need either the flow or typescript plugin.

    parser.parse("import type {Element} from 'react-devtools-shared/src/frontend/types';", {
      sourceType: "unambiguous",
      plugins: ['typescript']
    })
    

    See it on AST Explorer for reference.

    Configuration used in AST Explorer