microsoft / microsoft/TypeScript

Invalid import path rewrite in declaration file

Offen
#60,331 0 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: Declaration Emit Help Wanted
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 Search Terms

"import paths", "imports rewrite"

🕗 Version & Regression Information
  • This changed between versions 5.5.4 and 5.6.3
⏯ Repository with repro

https://github.com/MariaSolOs/path-rewrite-bug

💻 Code

The linked repository contains all of the necessary code to reproduce the issue. This is a pnpm monorepo with 4 packages.

mod1 simply exports the following interface and makes it available outside the package:

// mod1/index.ts
export interface Foo {
  foo: string;
}
// mod1/package.json
{
  ...
  "exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "default": "./dist/index.js"
    }
  }
}

mod2 depends on mod1 and exports its types.

// mod2/index.ts
import { Foo } from "mod1";

export const createFoo = (foo: string): Foo => ({ foo });

export type * from "mod1";
// mod2/package.json
{
  ...
  "exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "default": "./dist/index.js"
    }
  },
  "dependencies": {
    "mod1": "workspace:*"
  }
}

mod3 depends on mod2 and exports a simple getFoo function using the imported function from mod2:

// mod3/index.ts
import { createFoo } from "mod2";

export const getFoo = () => createFoo("foo");
// mod3/package.json
{
  ...
  "exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "default": "./dist/index.js"
    }
  },
  "dependencies": {
    "mod2": "workspace:*"
  }
}

Finally, mod4 depends on mod3 and uses getFoo to create another function:

// mod4/index.ts
import { getFoo } from "mod3";

export const foo = getFoo();
// mod4/package.json
{
  ...
  "exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "default": "./dist/index.js"
    }
  },
  "dependencies": {
    "mod3": "workspace:*"
  }
}
🙁 Actual behavior

Because mod3 has an invalid index.d.ts, mod4 fails compilation when doing a lib check of mod3's type declaration files. This is the invalid declaration file from mod3:

export declare const getFoo: () => import("mod1").Foo;
//# sourceMappingURL=index.d.ts.map
🙂 Expected behavior

For the behavior from previous versions of TypeScript to be maintained: Since mod2 re-exports mod1s types, and since mod3 depends on mod2, compiling mod3 should produce this:

export declare const getFoo: () => import("mod2").Foo;
//# sourceMappingURL=index.d.ts.map
Additional information about the issue

No response

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verknüpften path-rewrite-bug pnpm-Monorepo und baue die Pakete mod1 bis mod4 mit den im Issue beschriebenen Versionen. Überprüfe mod3s generiertes dist/index.d.ts und verifiziere, ob dessen Deklarationsimport Foo über mod2 statt über mod1 erfolgt. Die Aufgabe ist abgeschlossen, wenn die generierte Deklaration der erwarteten Ausgabe entspricht und mod4 die Kompilierung mit geprüften Deklarationsdateien besteht.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.