TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Support source phase imports

Open dsherret opened this issue 11 months ago • 2 comments

🔍 Search Terms

import source wasm

✅ 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

Support for source phase imports: https://github.com/tc39/proposal-source-phase-imports

  • https://github.com/nodejs/node/pull/56919
  • https://github.com/denoland/deno_core/pull/1081

📃 Motivating Example

// wasmModule is WebAssembly.Module
import source wasmModule from "./math.wasm"

💻 Use Cases

  1. Getting a wasm module instead of instance.

dsherret avatar Feb 18 '25 17:02 dsherret

If anybody is going to work on a PR for this, it might be worth doing it on top of https://github.com/microsoft/TypeScript/pull/60757 to avoid a ton of conflicts in the parsing logic.

nicolo-ribaudo avatar Feb 23 '25 23:02 nicolo-ribaudo

It would be good to make abstract module sources generic, especially for when sources support JS sources.

e.g.:

// mod.ts

export const name = "foo";

export function bar(): number { ... }
// mod has type ModuleSource<{ name: string, bar: () => number }>
import source modSource from "./mod.js";

// ... sometime later ...
const m = await import(modSource);

m.name; // has type string
m.bar; // has type () => number

For JS/TS/JSON the type of the module should already be available as it is today, ideally this would work with WASM as well but TS would need to parse wasm modules to find their types, alternatively we could have class WebAssembly.Module extends AbstractModuleSource<WebAssembly.Exports> if avoiding WASM parsing is really preferred.

Jamesernator avatar Feb 26 '25 07:02 Jamesernator