microsoft / microsoft/TypeScript

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

Open
#62,558 11 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: Declaration Emit Help Wanted Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

### 🔎 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the case from eslint.config.ts, node_modules/@eslint/config-helpers/index.d.ts, and tsconfig.json with composite enabled. Investigate how the compiler names the inferred default type when it uses the imported Config alias; done means the reduced example reports the expected TS2742 diagnostic without requiring a package change.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.