Skip to content

Improve JavaScript type from constructor imported via require #16686

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

Merged
merged 2 commits into from
Aug 25, 2017
Merged

Conversation

rbuckton
Copy link
Contributor

This improves the type we resolve for a new expression whose constructor was imported from a JavaScript file via a CommonJS require call.

Fixes #16467

@rbuckton rbuckton requested a review from yuit June 22, 2017 01:23
@rbuckton rbuckton added this to the TypeScript 2.4.2 milestone Jun 22, 2017
@rbuckton rbuckton requested a review from sandersn June 29, 2017 22:31
Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question about the const B = function ..., which looks to me like it's not testing the fix.

@@ -16089,6 +16089,21 @@ namespace ts {
return false;
}

function getJavaScriptClassType(symbol: Symbol): Type | undefined {
if (symbol && isDeclarationOfFunctionOrClassExpression(symbol)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the caller just checked that symbol is defined, so symbol && shouldn't be needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or it should be Symbol | undefined then

if (isJavaScriptConstructor(symbol.valueDeclaration)) {
return getInferredClassType(symbol);
}
if (symbol.flags & SymbolFlags.Variable) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's an example for this case? it looks like the const B = function ... case in the new test file, but I expected that to be imported from a new, third file instead of just being in the original file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The const A = case tests the from an external file, while the const B = case tests the use locally. Both cases are relevant. Are you talking about a third case where we import something re-exported from a third file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does line 16096 handle the const A = test (external) and 16099 handle the const B = test (local)? The two tests differ in syntax, so I thought the difference in the two cases in getJavascriptClassType was syntactic, and that both needed to come from external files. I guess that's wrong, and the difference is external vs local?

Copy link
Contributor Author

@rbuckton rbuckton Aug 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 16096 handles the case where symbol refers directly (globally or within the same file) to a function or a variable declaration whose initializer is a function.

Line 16099 handles the case where symbol refers indirectly to a function or variable declaration whose initializer is a function. This is usually the case for imports like const C = require("./c") or const { C } = require("./c"), but also cases like const C = B where B is also local and is a function or variable declaration whose initializer is a function.

@DanielRosenwasser
Copy link
Member

🔔 Status?

@mhegazy mhegazy removed this from the TypeScript 2.5 milestone Aug 16, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Aug 24, 2017

We will need to port this to release-2.5

@rbuckton rbuckton merged commit 4c68b6d into master Aug 25, 2017
@rbuckton
Copy link
Contributor Author

This is now also in release-2.5.

@rbuckton rbuckton deleted the fix16467 branch August 25, 2017 00:01
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

js intellisense issues about the custom export module.
6 participants