Skip to content

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

Open
@dsherret

Description

@dsherret

πŸ” 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.

function getResolvedModule(file: SourceFile, moduleName: string, mode: ResolutionMode) {
return resolvedModules?.get(file.path)?.get(moduleName, mode);
}

πŸ“ƒ 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: Import with type text, bytes, and URLΒ whatwg/html#9444)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions