Microsoft/TypeScript

Invalid import path rewrite in declaration file

Open

#60.331 geöffnet am 23. Okt. 2024

Auf GitHub ansehen
 (0 Kommentare) (3 Reaktionen) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
BugDomain: Declaration EmitHelp Wanted

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

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

Contributor Guide