【问题标题】:Eslint throwing "Parsing error: Unexpected token" while using TypeScriptEslint 在使用 TypeScript 时抛出“解析错误:意外的令牌”
【发布时间】:2022-10-09 01:10:04
【问题描述】:

我安装了@types/greensock 并开始使用gsapTypeScript。一切正常,但ScrollTo 插件给出了这个eslint 错误(附图片)。有人知道吗?

这是 eslint 错误:

这是我的.eslintrc

{
  "extends": "eslint:recommended",
  "parser": "babel-eslint",
  "env": {
    "node": true,
    "es6": true,
    "browser": true
  }
}

这是我的代码:

import { ScrollToPlugin } from "gsap/ScrollToPlugin";
import { gsap } from "gsap";
gsap.registerPlugin(ScrollToPlugin);

export const headerInteractionHandler = () => {
  document.querySelector("header .logo").addEventListener("click", (e) => {
    e.preventDefault();
    let element = <HTMLAnchorElement>e.currentTarget;

    gsap.to(window, {
      duration: 0.8,
      scrollTo: `${element.getAttribute("data-destination")}`,
    });
  });
};

【问题讨论】:

  • 这是解析错误,而不是 lint 错误。你能给我们你的.eslintrc吗?
  • @MatthieuRiegler 我添加了它。感谢您的时间 !
  • 您能否确认将解析器更改为 @typescript-eslint/parser 可以解决问题?
  • 不,它没有。
  • 由于您要迁移到 Typescript,我仍然建议您使用 @typescript-eslint/parser 作为解析器,因为我将允许您进行类型检查。

标签: javascript typescript eslint gsap


【解决方案1】:

我在 GreenSock 论坛上被告知我不应该使用 @types/greensock,因为 gsap 已经内置了对 TypeScript 的支持。所以我删除了它,并更新了我的.eslintrc,将babel-eslint 替换为@typescript-eslint/parser,如下所示,问题就消失了。

npm i @typescript-eslint/eslint-plugin @typescript-eslint/parser --save-dev
{
  
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"],
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended"
  ]
}

【讨论】:

    猜你喜欢
    • 2019-12-25
    • 2016-07-13
    • 2022-01-02
    • 2017-11-21
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多