-
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
Fix issue #22923 #23050
Fix issue #22923 #23050
Conversation
d789ce0
to
49a2599
Compare
src/compiler/checker.ts
Outdated
@@ -16409,6 +16409,9 @@ namespace ts { | |||
if (suggestion !== undefined) { | |||
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2, declarationNameToString(propNode), typeToString(containingType), suggestion); | |||
} | |||
else if (isPromiseLike(containingType)) { |
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.
call getPromisedTypeOfPromise
instead. this will return you the type of the T
in Promise<T>
. this should handle unions and custom promises, etc.. if it is undefined
then it was not a promise.
you also need to check if the property exists on the promisedType. in other words:
var p: Pomise<number>;
p.toExponential; // should report error `Property toExponential does not exist on type 'Promise<number>' did you forget `await`?`
var x: Promise<string>;
p.toExponential; // should not report the `did you forget await` part.
So, I've fixed a check plus have dropped tests for |
src/compiler/diagnosticMessages.json
Outdated
@@ -2000,7 +2000,10 @@ | |||
"category": "Error", | |||
"code": 2569 | |||
}, | |||
|
|||
"Property '{0}' does not exist on type '{1}'. Did you forget to await the '{1}'?": { |
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 Property '{0}' does not exist on type '{1}'. Did you forget 'await' ?
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.
@DanielRosenwasser can i get your input on this error message as well.
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.
Property '{0}' does not exist on type '{1}'. Did you forget to use 'await'?
is probably the best one actually.
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.
Or potentially
Property '{0}' does not exist on type '{1}', but does exist on type '{2}'. Did you forget to use 'await'?
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 had this at #23058. So which message?
- Property '{0}' does not exist on type '{1}'. Did you forget to use 'await'?
- Property '{0}' does not exist on type '{1}', but does exist on type '{2}'. Did you forget to use 'await'?
src/compiler/checker.ts
Outdated
if (suggestion !== undefined) { | ||
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2, declarationNameToString(propNode), typeToString(containingType), suggestion); | ||
} | ||
else if (promisedType && getPropertyOfType(promisedType, propNode.escapedText)) { |
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.
nit, consider moving const promisedType = getPromisedTypeOfPromise(containingType);
inside an else
block for if (suggestion !== undefined)
. it just makes sure we only compute it if we need it.
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.
Close to perfect. Can you also add the following to your tests?
- try accessing a
string
property onstring | Promise<string>
- use an element access to access a property on the awaited type.
src/compiler/checker.ts
Outdated
@@ -16410,7 +16410,13 @@ namespace ts { | |||
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2, declarationNameToString(propNode), typeToString(containingType), suggestion); | |||
} | |||
else { | |||
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1, declarationNameToString(propNode), typeToString(containingType)); | |||
const promisedType = getPromisedTypeOfPromise(containingType); |
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 would say that you should do the promise check before any sort of spelling checks because if a property exists on the awaited type, that's almost always a preferable suggestion.
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.
Will do
this is clear
What do you mean by element access? Something like |
element access is |
Ok, I've updated a message and have added the case for
As for element access I can not get what should I test. E.g.
will not emit even an old error, as they are in different branches - Or do you mean something like this?
|
I meant something like |
there is no error here for using a property that does not exist. the only error is under |
thanks @gagoman! |
Yes, I've tested this case. It is not covered by this branch. |
Fixes #22923
I'm still not sure that
isPromiseLike
should useisReferenceToType
or something more smart (e.g.getPromisedTypeOfPromise
to resolve actual types)