-
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
No error for require, module.exports or exports in Javascript #23743
Conversation
Still errors for module and exports, and require's type is now incorreclty 'any'; I broke module resolution somehow. Needs investigation.
Everything passes the tests but the code can be improved
if (!result) { | ||
if (originalLocation && isInJavaScriptFile(originalLocation) && originalLocation.parent) { | ||
if (isRequireCall(originalLocation.parent, /*checkArgumentIsStringLiteralLike*/ false)) { | ||
return requireSymbol; |
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.
we shoudl change all other uses in the checker for isRequireCall
to use the symbol identity check instead.
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.
But you only get requireSymbol
when @types/node
(or other declaration) is not present. See the rest of isCommonJsRequire
for the verification that's needed on declarations from things like @types/node
.
src/compiler/checker.ts
Outdated
@@ -4671,6 +4688,10 @@ namespace ts { | |||
if (symbol.flags & SymbolFlags.Prototype) { | |||
return links.type = getTypeOfPrototypeProperty(symbol); | |||
} | |||
// CommonsJS require/module/exports all have type any. | |||
if (symbol === requireSymbol || symbol === moduleSymbol || symbol === exportsSymbol) { | |||
return links.type = anyType; |
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.
type of export is not any
, it is the type of the module object. @andy-ms had a related change a while back, we should consolidate the two.. #17745
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.
Oops. Thanks for catching that.
It wasn't doing anything anyway.
Fixes #21933, although I still intend to add a quick fix for Typescript that runs
npm install @types/node
, and a quick fix for require("fs") that does the same thing. (That second quick fix would apply to JS and TS.)Note that I removed declarations for
require/module/exports
from a couple of the JS tests. There are plenty that still have their own declarations, which tests the rest of the code inisCommonJsRequire
after line 18772.