microsoft / microsoft/TypeScript

TS2742 “The inferred type of 'default' cannot be named” with ESLint `defineConfig`

未关闭
#62,558 11 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Domain: Declaration Emit Help Wanted Possible Improvement
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### 🔎 Search Terms

TS2742, 2742, composite, pnpm

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about TS2742

### ⏯ Playground Link

_No response_

### 💻 Code

**`eslint.config.ts`**

```ts
import { defineConfig } from "@eslint/config-helpers";
export default defineConfig();
```

**`node_modules/@eslint/config-helpers/index.d.ts`**

```ts
export type Config = import("@eslint/core").ConfigObject;
export function defineConfig(): Config;
```

**`node_modules/@eslint/config-helpers/node_modules/@eslint/core/index.d.ts`**

```ts
export interface ConfigObject {}
```

**`tsconfig.json`**

```json
{
"compilerOptions": {
"composite": true
}
}
```

(No other files are needed for this test case; there’s nothing else in `node_modules`.)

### 🙁 Actual behavior

No error.

### 🙂 Expected behavior

`eslint.config.ts(2,1): error TS2742: The inferred type of 'default' cannot be named without a reference to '@eslint/config-helpers/node_modules/@eslint/core'. This is likely not portable. A type annotation is necessary.`

### Additional information about the issue

In eslint/rewrite#283 I reported an unexpected error TS2742 when using the `defineConfig` function from ESLint. I reduced this test case to a clear TypeScript issue, which I’m reporting as requested in https://github.com/microsoft/TypeScript/issues/42873#issuecomment-2065572017.

This change makes the error disappear:

```diff
--- a/node_modules/eslint/node_modules/@eslint/config-helpers/index.d.ts
+++ b/node_modules/eslint/node_modules/@eslint/config-helpers/index.d.ts
@@ -1,2 +1,3 @@
-export type Config = import("@eslint/core").ConfigObject;
+import { ConfigObject as Config } from "@eslint/core";
+export { Config };
export function defineConfig(): Config;
```

However, in the real `@eslint/config-helpers` package, the line `export type Config = import("@eslint/core").ConfigObject` is generated by `tsc` from JSDoc `/** @typedef {import("@eslint/core").ConfigObject} Config */`, and there doesn’t seem to be a way to generate an equivalent change from JSDoc.

In any case, such a change shouldn’t be necessary. TypeScript ought to recognize that `export type Config = import("@eslint/core").ConfigObject` is already sufficient to allow the `defineConfig` return type to be named.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

在启用 composite 的情况下,复现 eslint.config.ts、node_modules/@eslint/config-helpers/index.d.ts 和 tsconfig.json 中的案例。调查编译器在使用导入的 Config 别名时如何命名推断出的默认类型;当精简示例无需修改包即可报告预期的 TS2742 诊断时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
冷清
描述清晰度
描述清楚
新手友好度
48/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。