microsoft / microsoft/TypeScript

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

Aperta
#62,558 11 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: Declaration Emit Help Wanted Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

🔎 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

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

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

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

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

export interface ConfigObject {}

tsconfig.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:

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci il caso da eslint.config.ts, node_modules/@eslint/config-helpers/index.d.ts e tsconfig.json con composite abilitato. Analizza come il compilatore denomina il tipo predefinito dedotto quando usa l'alias Config importato; il lavoro è completato quando l'esempio ridotto segnala la diagnostica TS2742 prevista senza richiedere una modifica al package.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Specificata chiaramente
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.