-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Can’t resolve @types
when vendoring dependencies for browser ESM imports
#50600
Comments
The initial problem linked to doesn't really seem to have anything to do with these issues. Like the problem here is that this doesn't work right? // Could not find a declaration file for module './node_modules/three/src/Three.js'. '/home/jamesernator/projects/playground/node_modules/three/src/Three.js' implicitly has an 'any' type.ts(7016)
import * as THREE from './node_modules/three/src/Three.js' But that this does, but this isn't syntactically valid for browsers: // @types/three work
import * as THREE from 'three' In principle, TypeScript probably could support the former by noticing An alternative solution would be to use import maps, although they're currently Chrome only, but if that limitation doesn't bother you OR you don't mind waiting for other browsers to support them then you could just do: <script type="importmap">
{
"three/": "./path/to/node_modules/three/"
}
</script> In which case (For libraries more complicated than three.js such an import map is a bit too simple, in which case you'd need an actual |
Thanks for the quick reply! Yes, the linked issues are maybe only related but not the same (not that I've exhaustively read all the replies therein).
This would require that THREE is installed as a node module right? Seems common but for something this fundamental still a limitation. (I believe I ended up making my own copy of THREE to get the modules in es6 style, so it's not on npm 🤷♂️). I will readily admit my use-case is not a common one :). It's just that when I'm working on a hobby project, the page of ~15 possible build tools to research is one I want to avoid for a while, ha! TIL about import maps! With a slight syntax tweak, your example worked perfectly:
Seems like a reasonable work around - probably by the time I want to support other browsers I'll have to add a JS build pipeline anyway.. |
Small nitpick: It is syntactically valid, it's just semantically invalid (unless the browser supports import maps, as you note). This is just a pet peeve of mine so take it with a grain of salt. 😅
Trying to make myself a bit more useful, I wanted to chime in on this that you can use es-module-shims to get import maps working on non-Chromium browsers. I've been using this in Oozaru, it works quite well. (@pehrlich2 this might be useful for you as well) |
@types
when vendoring dependencies for browser ESM imports
My current thoughts are
|
After thinking about it for about 30 seconds, I’m wondering if it needs to be more complicated than {
"compilerOptions": {
"relativePaths": {
"./node_modules/*": ["./node_modules/*", "./node_modules/@types/*"]
}
}
} where unlike |
Ok, so the problem with doing this with node_modules / npm packages specifically is that package.json files can contain complex intra-package mappings for types in themselves, so while the scheme I wrote above might work for relative paths in general, types inside packages cannot always be linked up just by relative paths. With this scheme, you’d immediately hit two different kinds of trouble:
There’s no way around stuff in node_modules being special; even if your module specifier into it doesn’t use any special Node resolution magic, we won’t be able to find types without using some of those things. So the question is, for a mode like |
I don't have an example on hand, but can't this be fixed by a |
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
⏯ Playground Link
N/A (behavior is documented)
💻 Code
Related frustrations have been posted many times before - so I've hesitated to post this:
#16577
#13422
#28288
#42151
My case, and workaround, are documented here: https://stackoverflow.com/questions/67725840/how-can-i-use-threejs-es6-native-modules-with-typescript/73575993#73575993
🙁 Actual behavior
import * as THREE from 'three'
🙂 Expected behavior
There could be multiple possible solutions here:
Just document it better, here: https://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html If this was described as a known limitation, it would have saved me half a day.
Support aliases with extensions in one of two ways:
a) Allow the alias itself to be of the final expected form
./js/lib/three.module.js
b) Allow some sort of a custom mapping, which I can use to tell tsc to look for an
.d.ts
aliasthree
when it sees./js/lib/three.module.js
Thanks in advance!
The text was updated successfully, but these errors were encountered: