microsoft / microsoft/TypeScript

Ability for checker to resolve modules by node similar to program when providing custom host

未关闭
#61,941 7 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Awaiting More Feedback Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

🔍 Search Terms

module resolution location

✅ Viability Checklist
⭐ Suggestion

The program provides the ability to resolve modules based on the specific string literal node, but the checker only resolves based on specifier text and node resolution mode. This means that if module specifiers with the same text and resolution mode resolve to different modules, the checker will always resolve to the same module based on whatever was last inserted into the program's ModeAwareCache.

https://github.com/microsoft/TypeScript/blob/47ca599979c418ade9b0a2f4107c305822472e44/src/compiler/program.ts#L1973-L1975

📃 Motivating Example
import text from "./data.txt" with { type: "text" };
import bytes from "./data.txt" with { type: "bytes" };
const { default: textDynamic }   = await import("./data.txt", { with: { type: "text" }});
const { default: bytesDynamic } = await import("./data.txt", { with: { type: "bytes" }});

let invalid: number;
invalid = text;
invalid = textDynamic;
invalid = bytes;
invalid = bytesDynamic;

let validText: string;
validText = text;
validText = textDynamic;

let validBytes: Uint8Array<ArrayBuffer>;
validBytes = bytes;
validBytes = bytesDynamic;

Currently outputs:

[ERROR]: Type 'Uint8Array<ArrayBuffer>' is not assignable to type 'number'.
invalid = text;
~~~~~~~
    at file:///V:/scratch/main.ts:7:1

TS2322 [ERROR]: Type 'Uint8Array<ArrayBuffer>' is not assignable to type 'number'.
invalid = textDynamic;
~~~~~~~
    at file:///V:/scratch/main.ts:8:1

TS2322 [ERROR]: Type 'Uint8Array<ArrayBuffer>' is not assignable to type 'number'.
invalid = bytes;
~~~~~~~
    at file:///V:/scratch/main.ts:9:1

TS2322 [ERROR]: Type 'Uint8Array<ArrayBuffer>' is not assignable to type 'number'.
invalid = bytesDynamic;
~~~~~~~
    at file:///V:/scratch/main.ts:10:1

TS2322 [ERROR]: Type 'Uint8Array<ArrayBuffer>' is not assignable to type 'string'.
validText = text;
~~~~~~~~~
    at file:///V:/scratch/main.ts:13:1

TS2322 [ERROR]: Type 'Uint8Array<ArrayBuffer>' is not assignable to type 'string'.
validText = textDynamic;
~~~~~~~~~
    at file:///V:/scratch/main.ts:14:1

Found 6 errors.

If I swap the last two dynamic imports then it will error on validBytes instead of validText.

💻 Use Cases
  1. Implementing bytes and text imports in Deno (which will be unstable and is currently non-standard: https://github.com/whatwg/html/issues/9444)

贡献指南

打开贡献指南

从这里开始

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

调研方向

先阅读 src/compiler/program.ts 中约第 1973-1975 行引用的模块解析逻辑,然后跟踪 checker 如何解析动机示例中的导入。完成的标准是 checker 解析可以使用具体的模块说明符节点,从而相同的文本和解析模式可以解析到不同的模块。

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

评估

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

把新 issue 发到你的邮箱

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