Repository navigation
Description
🔍 Search Terms
import attributes, import assertions
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
TypeScript 5.3 Beta supports the latest updates to the import attributes proposal, which is great to see!
It's possible to augment the global ImportAttributes type with host-specific attributes. However, auto-complete and type-checking of import attributes seems to only work with dynamic import(). It would be nice if this could also work for attributes in static declarations.
📃 Motivating Example
declare global {
interface ImportAttributes {
type: "json";
}
}
import * as ns from "foo" with { type: "not-json" }; // no error
void ns;
import("foo", {
with: {
type: "not-json", // Type '"not-json"' is not assignable to type '"json"'. (2322)
},
});💻 Use Cases
- What do you want to use this for?
https://github.com/tc39/proposal-import-attributes#motivation
In addition, we have an internal use case in Bloomberg to use Import Attributes as an annotation to achieve lazy-loading by deferring module evaluation. This is an early implementation of the TC39 proposal for Deferring Module Evaluation that annotates the static import.
It would be a better developer experience if auto-complete was available for Import Attributes in the static form.
- What shortcomings exist with current approaches?
It is possible to write a static import declaration using unknown/invalid attributes.
- What workarounds are you using in the meantime?
None.
Activity
RyanCavanaugh commentedon Oct 6, 2023
Consensus from the design meeting was that we just "forgot" to do this.
Additional point for type authors - use caution when defining
typelike this. The correct way to make an "open-ended union", as should be used here, is to make a dummy interface and use keyof so that other declarations can introduce new items to the union:krutoo commentedon Apr 9, 2026
This may not be the best place to ask, but could you tell me if it's possible to somehow describe types for modules that are imported specifically with attributes?
I tried something like this but it is not working:
jakebailey commentedon Apr 9, 2026
That is #46135, I searched for "module attribute" and it was the first result.