简体   繁体   English

获取解析错误:意外的令牌,预期的“;”

[英]Getting Parsing error: Unexpected token, expected “;”

I'm working on a big React + TypeScript project.我正在做一个大的React + TypeScript项目。

It works well on the local machine of other colleagues, but on mine I get the following error:它在其他同事的本地机器上运行良好,但在我的机器上我收到以下错误:

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

as you can see below:如下所示:

在此处输入图像描述

Here is the source code:这是源代码:

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");
  }
};

The commands I ran after cloning the repository were:克隆存储库后我运行的命令是:

$ yarn install
$ yarn start

Here there is some info about my system:这里有一些关于我的系统的信息:

$ node -v
v12.13.0

$ npm -v
6.12.0

$ yarn -v
1.19.1

Any idea on how to fix this?关于如何解决这个问题的任何想法?

Thanks!谢谢!

You are definitely on an old version of TypeScript.您肯定使用的是旧版本的 TypeScript。 Your syntax is perfectly valid.您的语法完全有效。

Tested经过测试

Example test done locally.在本地完成的示例测试。 在此处输入图像描述

A couple of things could be happening.可能会发生几件事。 Have you created a ts.config.js with ts configuration and parser config, something like:您是否使用 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 uses ESLint by default. Create React App 默认使用 ESLint。 However, ESLint can't parse TypeScript by default.但是 ESLint 默认无法解析 TypeScript。 If you want, you may consider using @typescript-eslint/parser.如果你愿意,你可以考虑使用@typescript-eslint/parser。

This could be that the base babel-eslint parser not working correctly without any config.这可能是基础 babel-eslint 解析器在没有任何配置的情况下无法正常工作。 ESLint is not applying different parsers to different files, hence babel-eslint might be throwing an error. ESLint 不会对不同的文件应用不同的解析器,因此 babel-eslint 可能会抛出错误。

Make sure your config file is created in the root of the project.确保您的配置文件是在项目的根目录中创建的。 I would start with that.我会从那开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM