microsoft / microsoft/TypeScript

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

Offen
#61,941 7 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

### 🔍 Search Terms

module resolution location

### ✅ Viability Checklist

- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ 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

```ts
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;
validBytes = bytes;
validBytes = bytesDynamic;
```

Currently outputs:

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

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

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

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

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

TS2322 [ERROR]: Type 'Uint8Array' 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)

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 damit, die zitierte Logik zur Modulauflösung in src/compiler/program.ts ungefähr in den Zeilen 1973-1975 zu lesen, und verfolge dann, wie der checker die Importe im motivierenden Beispiel auflöst. Als abgeschlossen gilt die Arbeit, wenn die checker-Auflösung den spezifischen Knoten des Modulspezifizierers verwenden kann, sodass identischer Text und Auflösungsmodus zu unterschiedlichen Modulen aufgelöst werden können.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

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