-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
AllowJs - ts-check - Cannot find namespace #19547
Comments
What is the shape of the actual package at runtime? If If it isn't available globally, then you won't be able to use it without importing its value in which case you will have access to the types. |
In my hypothetical example |
If it's not available globally than using
Different approaches are possible depending on how you load modules but let's say you're using CommonJS. var demo = require('demo');
/**
* @param x {demo.SomeType}
*/
function f(x) {} It's worth mentioning that there is a syntax error in the declaration in the OP so it will not work as written. |
Sorry, it appears that doesn't work. I thought it was supposed to work. var demo = require('demo');
/**
* @param {demo.SomeType} x // error: Cannot find namespace demo.
*/
function f(x) {} But it works correctly when imported as the synthetic default using ESM syntax. import demo from './demo';
/**
* @param {demo.SomeType} x // OK
*/
function f(x) {x} |
Unfortunate the oss ecosystem I am working on (webpack) still supports node 4 which is years away from ESM.. is there any other possible solution which doesn't require transpiling? |
No, but since are already using Webpack, you can just use the synthetic default form. Recent versions of Webpack perform the hoisting. I do think this is a bug however. |
I am not using webpack but developing a webpack plugin |
You could always use |
What might be a propper way to resolve this bug? |
You can potentially import into a .d.ts file that globally augments the type space. Also see |
@DanielRosenwasser I'm just wondering if this is the intended behavior. Perhaps it's a naive assumption but it seems like this scenario could be enabled and would be more natural for reusing existing declaration files. I realize that |
No, I do think this should just work. |
Thanks for all the additional information @DanielRosenwasser - #16489 and #18460 are almost duplicates for this topic and it seems the feature was added to the typescript 2.7 milestone by @mhegazy |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Code
node_modules/@types/thirdParty/index.d.ts
main.js
Expected behavior:
I should be able to use the type
foo
ofdemo
.The example from above would work if but only if the thirdParty plugin also exports its namespace:
I couldn't find any docs on what exactly
export as namespace
does.Proably it would just add it to the global namespace which might not be the desired solution.
Actual behavior:
Without the exported namespace I receive error messages which are not helpful at all:
Suggestion:
Could we import everything which is exported from the index.d.ts file (like
export = demo
) to a name?This would be useful for
--allowJs
checks where we can't import typings from another file.The text was updated successfully, but these errors were encountered: