Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support different typings with import attributes #56481

Closed
6 tasks done
jakearchibald opened this issue Nov 21, 2023 · 5 comments
Closed
6 tasks done

Support different typings with import attributes #56481

jakearchibald opened this issue Nov 21, 2023 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@jakearchibald
Copy link

jakearchibald commented Nov 21, 2023

πŸ” Search Terms

import attributes declare module types

βœ… Viability Checklist

⭐ Suggestion

With import attributes, you can now do this:

import AnImage from './icon.png' with { type: 'image' };
import AnArrayBuffer from './icon.png' with { type: 'arrayBuffer' };

…where a build tool (such as Rollup) will generate the modules.

What's missing is a way to provide the types, so TypeScript sees AnImage as (for example) an ImageBitmap, and AnArrayBuffer as an ArrayBuffer.

One suggestion, adapted from #53656 (comment):

declare module '*' with { type: 'image' } {
  const image: ImageBitmap;
  export default image;
}

However, it would also be nice if declared exports could be conditional. So in ./icon.png.d.ts:

declare const buffer: ArrayBuffer;
declare const image: ImageBitmap;

export for { type: 'image' } default image;
export for { type: 'arrayBuffer' } default buffer;

πŸ“ƒ Motivating Example

As described in the suggestion.

πŸ’» Use Cases

  1. What do you want to use this for?

I think this is covered in the suggestion.

  1. What shortcomings exist with current approaches?

It's common for with { type: "value" } to influence the types of the returned module, but TypeScript doesn't allow the types to differ.

  1. What workarounds are you using in the meantime?

Avoid using import attributes for this use-case, and instead use custom schemes in the module ID (or custom query strings).

@fatcerberus
Copy link

Wow, import attributes do add quite a bit of complexity to module loading, don't they... I'm guessing this can of worms is what TC39 were attempting to avoid when they originally specified asserts as not affecting how a module is loaded...

@andrewbranch
Copy link
Member

Thanks @jakearchibald; could I get you to post this feedback in #46135? We discussed this recently in #56359 and we’re looking for more information about existing real-world use cases, but I’d like to consolidate the discussion there.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Nov 21, 2023
@jakearchibald
Copy link
Author

Ugh, sorry, I promise I searched for an existing issue.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 24, 2023
@jakearchibald
Copy link
Author

Follow-up post #46135 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants