有什么方法可以解决 typescript-eslint 和 eslint 之间的类型不匹配问题吗?

问题描述 投票:0回答:1

我正在尝试将 ESLint 与 typescript-eslint 一起使用,如下所示:

import js from "@eslint/js";
import { ESLint } from "eslint";
import tseslint from "typescript-eslint";

const options = {
    overrideConfigFile: true,
    overrideConfig: tseslint.config(js.configs.all, tseslint.configs.all),
};

const eslint = new ESLint(options);
const fullConfig = (await eslint.calculateConfigForFile("src/index.js"));

问题是,这无法编译,因为

tseslint.config()
的返回类型与
overrideConfig
构造函数期望的
ESLint
类型不匹配。有什么方法可以让这项工作无需转换为预期类型

附注我收到以下错误消息:

Argument of type '{ overrideConfigFile: boolean; overrideConfig: FlatConfig.ConfigArray; }' is not assignable to parameter of type 'Options'.
  Types of property 'overrideConfig' are incompatible.
    Type 'ConfigArray' is not assignable to type 'Config<RulesRecord> | Config<RulesRecord>[] | null | undefined'.
      Type 'Config[]' is not assignable to type 'Config<RulesRecord>[]'.
        Type 'Config' is not assignable to type 'Config<RulesRecord>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
          The types of 'languageOptions.parser' are incompatible between these types.
            Type 'LooseParserModule | undefined' is not assignable to type 'Parser | undefined'.
              Type '{ meta?: { name?: string | undefined; version?: string | undefined; }; parseForESLint(text: string, options?: unknown): { ast: unknown; scopeManager?: unknown; services?: unknown; visitorKeys?: unknown; }; }' is not assignable to type 'Parser | undefined'.
                Type '{ meta?: { name?: string | undefined; version?: string | undefined; }; parseForESLint(text: string, options?: unknown): { ast: unknown; scopeManager?: unknown; services?: unknown; visitorKeys?: unknown; }; }' is not assignable to type 'Omit<ESTreeParser, "parseForESLint"> & { parseForESLint(text: string, options?: any): Omit<ESLintParseResult, "ast" | "scopeManager"> & { ...; }; }' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
                  Type '{ meta?: { name?: string | undefined; version?: string | undefined; }; parseForESLint(text: string, options?: unknown): { ast: unknown; scopeManager?: unknown; services?: unknown; visitorKeys?: unknown; }; }' is not assignable to type '{ parseForESLint(text: string, options?: any): Omit<ESLintParseResult, "ast" | "scopeManager"> & { ast: unknown; scopeManager?: unknown; }; }'.
                    The types returned by 'parseForESLint(...)' are incompatible between these types.
                      Type '{ ast: unknown; scopeManager?: unknown; services?: unknown; visitorKeys?: unknown; }' is not assignable to type 'Omit<ESLintParseResult, "ast" | "scopeManager"> & { ast: unknown; scopeManager?: unknown; }'.
                        Type '{ ast: unknown; scopeManager?: unknown; services?: unknown; visitorKeys?: unknown; }' is not assignable to type 'Omit<ESLintParseResult, "ast" | "scopeManager">'.
                          Types of property 'visitorKeys' are incompatible.
                            Type 'unknown' is not assignable to type 'VisitorKeys | undefined'.ts(2345)
const options: {
    overrideConfigFile: boolean;
    overrideConfig: FlatConfig.ConfigArray;
}
typescript-eslint
1个回答
0
投票

自从迁移到 ESLint v9 以来,这个错误是新的......

从 v8 到 v9 的一点更改表示重大更改,因此预计您的 v8 代码可能无法工作。 v9 迁移指南 中的任何内容有帮助吗?

这看起来就是罪魁祸首; ESLint 发生重大变化

正如我们在博客文章中所宣布的,临时 FlatESLint 类已重命名为 ESLint,而 v8.x 中的 ESLint 类已重命名为 LegacyESLint。

要解决的问题:如果您当前正在使用 ESLint 类,请使用新的 ESLint 类验证您的测试是否通过。并非所有旧选项都受支持,因此您可能需要更新传递给构造函数的参数。有关详细信息,请参阅 Node.js API 参考。

如果您仍然需要 v8.x ESLint 功能,请使用 LegacyESLint 类,如下所示:

const { LegacyESLint } = require("eslint/use-at-your-own-risk");

尝试将 ESLint 更改为 LegacyESLint,然后尝试为新的 ESLint 重写代码,这样您就不会依赖旧版行为。 他们的博客文章说...

仍将提供 LegacyESLint 类以允许访问 eslintrc 功能,但我们不会在 v9.x 中更新此类。所有新功能将仅针对基于平面配置的 API 实现。

© www.soinside.com 2019 - 2024. All rights reserved.