-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Fix exported type resolution in commonjs #24495
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
Conversation
It is fine to resolve the types of exported classes in ES6: ```js export class C { } var c = new C() ``` But not for commonjs exported classes: ```js module.exports.C = class { } var c = new C() // should error ``` Fixes #24492
@mhegazy do you want this in 2.9 too? |
src/compiler/checker.ts
Outdated
if (name !== InternalSymbolName.Default && (result = lookup(moduleExports, name, meaning & SymbolFlags.ModuleMember))) { | ||
break loop; | ||
if ((location as SourceFile).commonJsModuleIndicator && !result.declarations.some(isJSDocTypeAlias)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be better to do this check before calling lookup
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also check that the location is a soruceFile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, although it would be really weird for a ModuleDeclaration to have a property named commonJsModuleIndicator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Andy-MS I still want to call lookup, even for commonjs modules, because it might find a jsdoc type alias. I can't unconditionally skip commonjs modules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(believe me, I tried :)
Please port to release-2.9 as well. |
* Fix resolution of exported types in commonjs It is fine to resolve the types of exported classes in ES6: ```js export class C { } var c = new C() ``` But not for commonjs exported classes: ```js module.exports.C = class { } var c = new C() // should error ``` Fixes #24492 * All jsdoc type aliases are available locally in commonjs modules * Check that location isSourceFile before commonJsModuleIndicator
OK, it's in 2.9 now too. |
It is fine to resolve the types of exported classes in ES6:
But not for commonjs exported classes:
JSDoc type aliases are an exception since they are always exported implicitly, not by [property] assignment to
module.exports
orexports
.Fixes #24492