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

Typed JSON imports #60037

Closed
6 tasks done
sdegutis opened this issue Sep 23, 2024 · 3 comments
Closed
6 tasks done

Typed JSON imports #60037

sdegutis opened this issue Sep 23, 2024 · 3 comments

Comments

@sdegutis
Copy link

πŸ” Search Terms

json import module type

βœ… Viability Checklist

⭐ Suggestion

Allow specifying the type of dynamically created and imported JSON files with .d.ts files.

πŸ“ƒ Motivating Example

import icons from '/icons.json' with { type: 'json' };

This is a type error when /icons.json doesn't exist statically. There's no way to give it a type currently.

πŸ’» Use Cases

  1. What do you want to use this for?

Give /icons.json a type when imported though it's dynamically generated.

  1. What shortcomings exist with current approaches?

/icons.json doesn't have a type, and is a type error.

  1. What workarounds are you using in the meantime?

Using fetch with a then-generic-type.

@SebastienGllmt
Copy link

I think this is already supported: #32063

@MartinJohns
Copy link
Contributor

I think this is already supported: #32063

That would require the file to be existing, which, if I understand OP correctly, is not the case.

Normally you would create a declaration file and provide the types there, but it does not seem to work with a leading / character (and I don't know why). For an import from icons.json instead of /icons.json it works fine.

@sdegutis
Copy link
Author

It doesn't require the file to exist, and it actually worked for me, with allowArbitraryExtensions: true. Solved, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants