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

Error when generating definition from javascript (error TS9006) #47614

Closed
llgcode opened this issue Jan 26, 2022 · 2 comments · Fixed by #47835
Closed

Error when generating definition from javascript (error TS9006) #47614

llgcode opened this issue Jan 26, 2022 · 2 comments · Fixed by #47835
Assignees
Labels
Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files Domain: JS Emit The issue relates to the emission of JavaScript Fix Available A PR has been opened for this issue

Comments

@llgcode
Copy link

llgcode commented Jan 26, 2022

Bug Report

Hi,
our goal is to generate definition from javascript files.It will be helpful for checking types and have completion.

Our workspace environmnent is a multi project that declare project dependencies using references property in tsconfig.

It is stopped in our case with this error TS9006: Declaration emit for this file requires using private name 'BaseClass'.

We couldn't find a satisfactory workaround.

Can we say that it is a limitation of typescript ?

Thanks

🔎 Search Terms

error TS9006: Declaration emit for this file requires using private name

Not sure if this is the same issue as #41672

🕗 Version & Regression Information

No regression. It is something that we started to work on.
The issue is reproducible with typescript 4.5.5 and 4.6.0-beta.

⏯ Playground Link

This minimal repo illustrate our case and highlight the issue:
https://github.com/llgcode/js-def-issue

💻 Code

declare module "deps/BaseClass" {
    class BaseClass {
        static extends<A>(a: A): new () => A & BaseClass;
    }
    export = BaseClass;
}
define("lib/ExtendedClass", ["deps/BaseClass"], 
/**
 * {typeof import("deps/BaseClass")}
 * @param  {typeof import("deps/BaseClass")} BaseClass 
 * @returns 
 */
(BaseClass) => {    
    
    const ExtendedClass = BaseClass.extends({
        f: function() {
            return "something";
        }
    });

    // Exports the module in a way tsc recognize class export 
    const module = {};
    module.exports = ExtendedClass
    return module.exports;
});

🙁 Actual behavior

ExtendedClass.js:1:1 - error TS9006: Declaration emit for this file requires using private name 'BaseClass' from module '"deps/BaseClass"'. An explicit type annotation may unblock declaration emit.

1 define("lib/ExtendedClass", ["deps/BaseClass"],
  ~~~~~~

🙂 Expected behavior

Generate the definition file.

@weswigham
Copy link
Member

Right, so comment one: we don't really support AMD module wrappers in our input modules, afaik. I mean, we'll typecheck them as best we can, but they aren't specially recognized as defining or being modules or anything. (In fact, you'd have to write a amd-module triple-slash comment to tell us the module name if it's needed in any output). So with that out of the way, let's talk about what's going on here. We are trying to generate a module declaration file for ExtendedClass.js, because for some reason, we pick up the nested module.exports reference and see it as an indicator that the file is a module (?!? I thought we only detected these outside of function bodies...). I guess your comments indicate you know this and are exploiting it to your own ends. With that all explained, we come to the meat of the issue: Why is BaseClass not accessible? Turns out our declaration emit symbol accessibility checking logic can't currently detect that the target of an ambient module's export = should be considered accessible via said module. A relatively simple fix, now up at #47835

@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Feb 10, 2022
@weswigham weswigham added Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files Domain: JS Emit The issue relates to the emission of JavaScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Feb 10, 2022
@llgcode
Copy link
Author

llgcode commented Feb 14, 2022

Thanks @weswigham. :-)
What would be the right way to define an AMD module to have a definition generated in the right way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files Domain: JS Emit The issue relates to the emission of JavaScript Fix Available A PR has been opened for this issue
Projects
None yet
4 participants