-
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
Prefer elaborating on expressions which could be called to produce a correct type by suggesting such #27016
Conversation
…correct type by suggesting such
src/compiler/checker.ts
Outdated
const callSignatures = getSignaturesOfType(source, SignatureKind.Call); | ||
const constructSignatures = getSignaturesOfType(source, SignatureKind.Construct); | ||
for (const signatures of [constructSignatures, callSignatures]) { | ||
if (length(signatures)) { |
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.
Do you need the length()
call with some
?
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.
Probably not.
src/compiler/checker.ts
Outdated
@@ -10585,6 +10585,9 @@ namespace ts { | |||
|
|||
function elaborateError(node: Expression | undefined, source: Type, target: Type): boolean { | |||
if (!node || isOrHasGenericConditional(target)) return false; | |||
if (!isTypeAssignableTo(source, target) && elaborateDidYouMeanToCallOrConstruct(node, source, target)) { |
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 should ideally be cheap since the original call will have some of the relationships cached, right?
Also, technically I think you should be using the original relationship, not assignability, right?
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.
Yes and yes.
src/compiler/checker.ts
Outdated
return !(returnType.flags & (TypeFlags.Any | TypeFlags.Never)) && isTypeAssignableTo(returnType, target); | ||
})) { | ||
const resultObj: { error?: Diagnostic } = {}; | ||
checkTypeAssignableTo(source, target, node, /*errorMessage*/ undefined, /*containingChain*/ undefined, resultObj); |
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.
TIL errorOutputContainer
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.
Yeah, I added that awhile ago. Not the happiest about it, but a side channel's a side channel, since most callers just need the boolean return.
src/compiler/checker.ts
Outdated
checkTypeAssignableTo(source, target, node, /*errorMessage*/ undefined, /*containingChain*/ undefined, resultObj); | ||
const diagnostic = resultObj.error!; | ||
addRelatedInfo(diagnostic, createDiagnosticForNode( | ||
node, |
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.
Kind of funny that the "related span" is the same span.
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.
I think it's still probably the best place to put it. Wanna do something else?
"category": "Message", | ||
"code": 6212 | ||
}, | ||
"Did you mean to use `new` with this expression?": { |
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.
new
[](start = 25, length = 5)
Elsewhere, "new" is in single quotes.
Fixes #25308
(Note that this adds the messages, not a potential quickfix. A quickfix should be more narrow than the suggestion, as only a 0-argument call/construct has an obvious quickfix)