-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Add JS-specific diagnostic message for resolve()
in Promise where type argument can't be inferred
#48533
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
403227f
to
d89aac4
Compare
src/compiler/diagnosticMessages.json
Outdated
@@ -3365,6 +3365,10 @@ | |||
"category": "Error", | |||
"code": 2809 | |||
}, | |||
"Expected {0} arguments, but got {1}. TypeScript may need a JSDoc hint that the call to 'new Promise()' produces a 'Promise<void>'": { |
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 believe this was the message suggested in the issue, so it make sense to use it, but this wording strikes me as off as I'm not sure that we have any error messages that effectively "blame" TypeScript itself for needing a hint about the code's behavior, at least not in this particular wording. I would have written something more like
This promise may need a JSDoc-style type that the call to 'new Promise()' produces a 'Promise'
To shift things around to pointing at the particular code that's at fault, not at the language itself.
But, this is bikeshedding. @DanielRosenwasser @andrewbranch
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 agree this message doesn’t quite fit. What exactly does the codefix for JS do? Does it add an annotation both for variable declarations and expressions (i.e. a type assertion)?
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.
It adds a type tag:
var a = /** @type {Promise<void>} */(new Promise(resolve => resolve()));
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.
How about
'new Promise()' needs a JSDoc hint to produce a 'resolve' that can be called without arguments.
'resolve'
could be substituted with an interpolation to use the parameter name the user wrote.- The first interpolation (
{0} arguments
) is unnecessary as we know it’s0
.
(cc @DanielRosenwasser)
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.
Nice, this provides better feedback indeed :)
resolve()
in Promise where type argument can't be inferred
src/compiler/diagnosticMessages.json
Outdated
@@ -3365,6 +3365,10 @@ | |||
"category": "Error", | |||
"code": 2809 | |||
}, | |||
"Expected {0} arguments, but got {1}. TypeScript may need a JSDoc hint that the call to 'new Promise()' produces a 'Promise<void>'": { |
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 agree this message doesn’t quite fit. What exactly does the codefix for JS do? Does it add an annotation both for variable declarations and expressions (i.e. a type assertion)?
49d95c5
to
94dc46b
Compare
In a separate PR, we should consider expanding the codefix to cover /** @param {() => void} resolve */
function alwaysResolve(resolve) { resolve() }
new Promise(alwaysResolve); (both JS and TS versions) |
I see, it is complaining indeed now but no code fix is provided, I would be happy to help with this and open another PR later :) |
Yes. It’s a different error but the same fix can be applied. |
f300244
to
8a66d15
Compare
: Diagnostics.Expected_0_arguments_but_got_1; | ||
const isVoidPromiseError = !hasRestParameter && parameterRange === 1 && args.length === 0 && isPromiseResolveArityError(node); | ||
if (isVoidPromiseError && isInJSFile(node)) { | ||
return getDiagnosticForCallNode(node, Diagnostics.Expected_1_argument_but_got_0_new_Promise_need_a_JSDoc_hint_to_produce_a_0_that_can_be_called_without_arguments, "resolve"); |
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.
Is there any convenient way to get the name of the function? I tried symbolName(node.symbol)
without success
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.
resolveName(node.expression, node.expression.escapedText, SymbolFlags.Value, undefined, undefined, false)?.name
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.
It may also be reasonable just to call it resolve
since that’s the parameter name in the library file.
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.
Ok, I will change it to a straightforward string
src/compiler/diagnosticMessages.json
Outdated
@@ -3369,6 +3369,10 @@ | |||
"category": "Error", | |||
"code": 2809 | |||
}, | |||
"Expected 1 argument, but got 0. 'new Promise()' need a JSDoc hint to produce a {0} that can be called without arguments.": { |
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.
"Expected 1 argument, but got 0. 'new Promise()' need a JSDoc hint to produce a {0} that can be called without arguments.": { | |
"Expected 1 argument, but got 0. 'new Promise()' needs a JSDoc hint to produce a {0} that can be called without arguments.": { |
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.
Oh sorry for this!
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.
Thank you!
Fixes: #46570
Continues #46637
I have cherry picked the commit from there.
Address the comments of the above PR, restricting the change to JS files.