You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Default imported function merged with namespace cannot be called (has no call signatures) #54342
This expression is not callable.
Type 'typeof replaceInFile' has no call signatures. (ts2349)
🙂 Expected behavior
replaceInFile should be callable. It's a function and even is known to have function properties such as .apply, .bind, and .call.
If there actually is something off about the code with respect to modules and importing, then the error should be made more clear. This is very confusing. 😞
Perhaps for clarity (and posterity) you should post the definition of replaceInFile? Right now the visible part of the repro is just default-importing a function from a module hosted on npm, which AFAIK does work in general, so it would help to see upfront what specifically is different about this case.
At the very least, it helps the reader to track down the definitions used by the given repro case. On top of that, the types acquisition algorithm used in the playground actually looks for those comments and tries to fetch the types for the referenced version.
While the behavior reported seems weird, the conclusion for now has been that it's not exactly a bug that you can't use replaceInFile as a function here. What happens is that, in the library's .d.ts file, the line export default replaceInFile is actually default exporting only the namespace replaceInFile, and not the export function replaceInFile. This happens because the namespace and function declaration don't merge, because they're not both exported or both not exported.
exportfunctionreplaceInFile(): Promise<void>{returnPromise.resolve(undefined);}// Error: Individual declarations in merged declaration 'replaceInFile' must be all exported or all local.(2395)exportdefaultreplaceInFile;namespacereplaceInFile{// Error: Individual declarations in merged declaration 'replaceInFile' must be all exported or all local.(2395)exportfunctionsync(): void{}}replaceInFile();// WorksreplaceInFile.sync();// Doesn't work, the two `replaceInFile` don't properly merge
So what we're probably going to try to do here is to give a similar error on a .d.ts file, to prevent people from running into this confusion.
With that declaration file the namespace is currently considered to be exported (regardless of the fact that it doesn't have an explicit export modifier on it) based on this condition:
if(!(flags&ModifierFlags.Ambient)&&!(isModuleBlock(n.parent)&&isModuleDeclaration(n.parent.parent)&&isGlobalScopeAugmentation(n.parent.parent))){// It is nested in an ambient context, which means it is automatically exportedflags|=ModifierFlags.Export;}
So the question is: is the real problem here that the diagnostic should be emitted here? Is the problem with that condition? Or is the problem with symbol merging (that it doesn't follow the same logic as this condition or smth like that)?
I have 0 intuition about this really since I always find namespaces, augmentations, etc pretty confusing and I just don't use them at all in my code.
Yeah, the namespace is automatically exported because everything in an ambient context is (except if you have an explicit export statement, which I guess the export default _ doesn't count for). Those rules are very confusing to me, too, they might be confusing for everybody in fact. The problem of why it doesn't merge is because the two declarations are different with regards to having the export modifier in the first place. My explanation above referred to the presence or absence of the export modifier on the declaration itself. The fact that the namespace is, after all, exported because of the export default replaceInFile, just makes it more confusing, but the thing is the declarations need to merge in order to export default replaceInFile export both the namespace and the function.
So what’s the solution here? Should that namespace not be treated as exported despite of being in an ambient context because the other declaration has an explicit export modifier? In other words - should the special rule related to ambient contexts only kick in when all declarations of a symbol (or all symbols?) within that context are export-less?
Made a mistake above: the export default replaceInFile statement does count and turns off implicit exports in the .d.ts file. I edited my previous comment to reflect that, sorry about the confusion.
those symbols here shouldn't be merged (as they are not today)
the appropriate diagnostic should be raised here ("Individual declarations in merged declaration 'replaceInFile' must be all exported or all local.(2395)")
Did I get this right? 😅
What about the importing site (essentially what this issue is about)? Should the imported value be callable? It feels to me that it should as the exported value is callable, right? It feel that the problem in the declaration file should make sync unavailable but not the exported function.
That sounds about right, yeah. About (1), to be more precise, the namespace should not be treated as exported on its own, i.e. it should not be treated as if it implicitly had an export modifier on its declaration.
I'm working on a PR to get the diagnostic to be raised in .d.ts files, I believe it currently isn't because of a bug, the bug being that in the checker, in the code you pointed out above, we consider everything in an ambient context to be exported, when that should not always be the case.
About the export default replaceInFile effectively exporting the namespace or the function, I'm not sure if there's a right answer. This will soon be an error and typically that means we don't provide guarantees about what will happen. But I don't have strong opinions on that.
Activity
fatcerberus commentedon May 22, 2023
Perhaps for clarity (and posterity) you should post the definition of
replaceInFile? Right now the visible part of the repro is just default-importing a function from a module hosted on npm, which AFAIK does work in general, so it would help to see upfront what specifically is different about this case.Andarist commentedon May 22, 2023
I already kinda copy-pasted those definitions into the test file here.
A neat trick is to use a magic~ comment in the playground, like this:
At the very least, it helps the reader to track down the definitions used by the given repro case. On top of that, the types acquisition algorithm used in the playground actually looks for those comments and tries to fetch the types for the referenced version.
JoshuaKGoldberg commentedon May 22, 2023
Updated to mention: this is from
replace-in-file. A minimum repro of their types would roughly be:Full
node_modules/replace-in-files/types/index.d.tsHuh nifty, TIL! Thanks 😄
gabritto commentedon Jun 14, 2023
While the behavior reported seems weird, the conclusion for now has been that it's not exactly a bug that you can't use
replaceInFileas a function here. What happens is that, in the library's.d.tsfile, the lineexport default replaceInFileis actually default exporting only thenamespace replaceInFile, and not theexport function replaceInFile. This happens because the namespace and function declaration don't merge, because they're not both exported or both not exported.If you write an equivalent thing in a
.tsfile, we do give you an error about it:So what we're probably going to try to do here is to give a similar error on a
.d.tsfile, to prevent people from running into this confusion.Andarist commentedon Jun 14, 2023
With that declaration file the
namespaceis currently considered to be exported (regardless of the fact that it doesn't have an explicit export modifier on it) based on this condition:So the question is: is the real problem here that the diagnostic should be emitted here? Is the problem with that condition? Or is the problem with symbol merging (that it doesn't follow the same logic as this condition or smth like that)?
I have 0 intuition about this really since I always find namespaces, augmentations, etc pretty confusing and I just don't use them at all in my code.
gabritto commentedon Jun 14, 2023
Yeah, the namespace is automatically exported because everything in an ambient context is (except if you have an explicit export statement, which I guess theThose rules are very confusing to me, too, they might be confusing for everybody in fact. The problem of why it doesn't merge is because the two declarations are different with regards to having theexport default _doesn't count for).exportmodifier in the first place. My explanation above referred to the presence or absence of theexportmodifier on the declaration itself. The fact that the namespace is, after all, exported because of theexport default replaceInFile, just makes it more confusing, but the thing is the declarations need to merge in order toexport default replaceInFileexport both the namespace and the function.Andarist commentedon Jun 14, 2023
So what’s the solution here? Should that namespace not be treated as exported despite of being in an ambient context because the other declaration has an explicit
exportmodifier? In other words - should the special rule related to ambient contexts only kick in when all declarations of a symbol (or all symbols?) within that context are export-less?gabritto commentedon Jun 14, 2023
Made a mistake above: the
export default replaceInFilestatement does count and turns off implicit exports in the.d.tsfile. I edited my previous comment to reflect that, sorry about the confusion.Andarist commentedon Jun 15, 2023
So to confirm, with this code:
Did I get this right? 😅
What about the importing site (essentially what this issue is about)? Should the imported value be callable? It feels to me that it should as the exported value is callable, right? It feel that the problem in the declaration file should make
syncunavailable but not the exported function.gabritto commentedon Jun 15, 2023
That sounds about right, yeah. About (1), to be more precise, the namespace should not be treated as exported on its own, i.e. it should not be treated as if it implicitly had an
exportmodifier on its declaration.I'm working on a PR to get the diagnostic to be raised in
.d.tsfiles, I believe it currently isn't because of a bug, the bug being that in the checker, in the code you pointed out above, we consider everything in an ambient context to be exported, when that should not always be the case.About the
export default replaceInFileeffectively exporting the namespace or the function, I'm not sure if there's a right answer. This will soon be an error and typically that means we don't provide guarantees about what will happen. But I don't have strong opinions on that.