-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Node module resolution resolving regardless of typings
field
#6427
Comments
a self contained repro will definitely be very helpful. |
I'm also not sure if I'm running into a different issue, but should Edit: Looks like I have a separate bug to fix, it's correct:
|
tl;dr; current behavior is by design. The first step that compiler is doing is building the list of files that it needs to compile. This list is computed by doing depth first traversal starting from root files ( in your case it will be In your case declare module 'popsicle' {
export * from 'popsicle/dist/common';
} compiler will try to resolve |
Why does it try to resolve it? It's already been defined as an ambient module. Is there a way around this? It kind of makes it impossible to publish a module that has dependencies on modules that don't have type definitions. Edit: I'd prefer not to have to hack support. |
at the step when compiler collects list of files it in general has no idea whether some module specifier correspond to ambient or real external module (i.e. because you have not yet seen file that will bring this ambient module into scope). What I can think of off the top of my head is something like this
Every module will effectively be split into two parts:
|
@vladima You can't do that (see edit 2) because mangling it means it'll break imports. The point of typings is to allow things like Edit: It's a common enough pattern to be able to do that and I don't want to break support for it. Even |
imports wont be broken since real module module "~~~react/addons" {
// used by imports inside this .d.ts file
}
module "react/addons" {
// used by user code, reexports everything from "~~~react/addons"
export * from "~~~react/addons";
} in user code |
@vladima Also, I don't quite understand the first paragraph - can you explain it even more plainly please? Wouldn't it be possible to introduce a restriction where it doesn't look for the file when the corresponding ambient declaration exists in the same file? To find those imports would mean it's already loaded the corresponding ambient declarations anyway, so I don't understand what it means when you say "because you have not yet seen file that will bring this ambient module into scope". |
I don't think the prefixing hack is reasonable or even possible. It'll only solve the import from within the ambient declaration itself, but not the users code. Once the user does Edit: Replace |
there are few reasons for current behavior:
// file1.ts
import {x} from "some-name"; // file2.ts
declare module "some-name" {
} If initial list of files was pinging @mhegazy to chime in. |
by the way since your package already bundles typings why can't you just use them? I'm not sure that I understand why do you need to repackage separate |
@vladima I can't use the built-in bundle because the dependencies don't have their own packaged typings. I believe I've discussed it offline with the TypeScript team before publishing Typings, and I'm available to do so again. As for the restriction above, I kind of understand - it's a little unfortunate though. Would it be possible to restrict module resolution behaviour when the ambient declaration is within the same file already? For example, declare module 'events' {
...
}
declare module 'fs' {
import * as events from 'events'
} |
I have good news that |
I'm happy to close this issue. I think it's less of a problem with the ability to now set |
I've run into this a bundle of times now and usually just force myself back to using classic module resolution, and looking into the code briefly yields no reason this should be happening.
Basically, I have a lot of modules like popsicle which does not specify a
typings
field inpackage.json
. However, once I install it using typings (which is just wrapping everything up into ambient module declarations) and use it, it starts to resolve the actual files even though the ambient definitions are already provided. I can only assume that there's some sort of declaration file merging doing on when using node module resolution, but some clarification would be good.Here's a snippet of the generated definition:
Notice that the file is being correctly rewritten. However, with
node
resolution enabled, it'll also load the original file too - even though nothing at all references it. This results in the original code generating compiler errors and having to switch back to classic.I can create a replicated repo, if needed, but I hope the issue sheds enough light on the problem.
The text was updated successfully, but these errors were encountered: