首页 > 解决方案 > 获取解析错误:意外的令牌,预期的“;”

问题描述

我正在做一个大React + TypeScript项目。

它在其他同事的本地机器上运行良好,但在我的机器上我收到以下错误:

Line 1:  Parsing error: Unexpected token, expected ";"

如下所示:

在此处输入图像描述

这是源代码:

export type LoadOfferType = typeof import("../offers/defaultOffer");

export const loadOffer = async (): Promise<LoadOfferType> => {
  const offerName = process.env.NODE_ENV === "development" ? process.env.REACT_APP_FALLBACK_SITE : "defaultOffer";

  /**
   * We use a switch statement instead of ane xpression for the offer path
   * because Webpack throws a critical dependency error when using paths
   * that are not explicitly defined.
   */
  switch (offerName) {
    case "mysite.com":
      return await import("../offers/mysite.com");
    default:
      return await import("../offers/defaultOffer");
  }
};

克隆存储库后我运行的命令是:

$ yarn install
$ yarn start

这里有一些关于我的系统的信息:

$ node -v
v12.13.0

$ npm -v
6.12.0

$ yarn -v
1.19.1

关于如何解决这个问题的任何想法?

谢谢!

标签: javascriptreactjstypescripttslint

解决方案


可能会发生几件事。您是否使用 ts 配置和解析器配置创建了 ts.config.js,例如:

 export const typescriptConfig = {
 extends: ['plugin:@typescript-eslint/eslint- 
 recommended'],
 overrides: [
   {
     parser: '@typescript-eslint/parser',
     extends: [
       'plugin:@typescript-eslint/recommended',
       'prettier/@typescript-eslint',
       'plugin:import/typescript',
     ],
     plugins: ['@typescript-eslint'],

     files: ['*.ts', '*.tsx'],

     rules: {},
   },
 ],

}

Create React App 默认使用 ESLint。但是,ESLint 默认无法解析 TypeScript。如果你愿意,你可以考虑使用@typescript-eslint/parser。

这可能是基础 babel-eslint 解析器在没有任何配置的情况下无法正常工作。ESLint 不会对不同的文件应用不同的解析器,因此 babel-eslint 可能会抛出错误。

确保您的配置文件是在项目的根目录中创建的。我会从那开始。


推荐阅读