-
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
JavaScript prototype class inference #5876
JavaScript prototype class inference #5876
Conversation
# Conflicts: # src/compiler/binder.ts # src/compiler/checker.ts # src/harness/fourslash.ts
|
||
// Look up the function in the local scope, since prototype assignments should immediately | ||
// follow the function declaration | ||
const funcSymbol = container.locals[classId.text]; |
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.
This doesn't guarantee that the prototype assignment immediately follows. Also, what if classId
denotes a non-function, such as a variable or an interface declaration? I'm thinking you should instead check that the prototype assignment is immediately contained in a statement list and that the immediately preceding statement is a function declaration.
I'm thinking that the kind of inference you're doing should also apply when you access |
Added |
@ahejlsberg any other comments? |
// in a JS file | ||
const funcSymbol = checkExpression(node.expression).symbol; | ||
if (funcSymbol && funcSymbol.members && (funcSymbol.flags & SymbolFlags.Function)) { | ||
return createAnonymousType(undefined, funcSymbol.members, emptyArray, emptyArray, /*stringIndexType*/ undefined, /*numberIndexType*/ undefined); |
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.
You need to create this type once and cache it. Right now you're creating a brand new type every time which allocates a lot of objects and defeats all the type comparison caching we do.
👍 |
JavaScript prototype class inference
🎉 |
Cleanup of #5578 that is based to the appropriate branch