microsoft / microsoft/TypeScript

Invalid import path rewrite in declaration file

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

还没有人认领这个 Issue。

Bug Domain: Declaration Emit Help Wanted
主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
117

描述

🔎 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

贡献指南

打开贡献指南

从这里开始

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

调研方向

从链接的 path-rewrite-bug pnpm monorepo 开始,使用 issue 中描述的版本构建 mod1 到 mod4 软件包。检查 mod3 生成的 dist/index.d.ts,并验证其声明是否通过 mod2 而不是 mod1 导入 Foo。当生成的声明与预期输出一致,并且 mod4 在检查声明文件的情况下通过编译时,即表示完成。

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

评估

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

把新 issue 发到你的邮箱

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