Skip to content

Commit da31239

Browse files
authored
Merge pull request #23050 from gagoman/fix/22923
Fix issue #22923
2 parents 2167b24 + 134b341 commit da31239

17 files changed

+154
-5
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16616,12 +16616,18 @@ namespace ts {
1661616616
}
1661716617
}
1661816618
}
16619-
const suggestion = getSuggestionForNonexistentProperty(propNode, containingType);
16620-
if (suggestion !== undefined) {
16621-
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2, declarationNameToString(propNode), typeToString(containingType), suggestion);
16619+
const promisedType = getPromisedTypeOfPromise(containingType);
16620+
if (promisedType && getPropertyOfType(promisedType, propNode.escapedText)) {
16621+
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_forget_to_use_await, declarationNameToString(propNode), typeToString(containingType));
1662216622
}
1662316623
else {
16624-
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1, declarationNameToString(propNode), typeToString(containingType));
16624+
const suggestion = getSuggestionForNonexistentProperty(propNode, containingType);
16625+
if (suggestion !== undefined) {
16626+
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2, declarationNameToString(propNode), typeToString(containingType), suggestion);
16627+
}
16628+
else {
16629+
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1, declarationNameToString(propNode), typeToString(containingType));
16630+
}
1662516631
}
1662616632
diagnostics.add(createDiagnosticForNodeFromMessageChain(propNode, errorInfo));
1662716633
}

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2012,7 +2012,10 @@
20122012
"category": "Error",
20132013
"code": 2569
20142014
},
2015-
2015+
"Property '{0}' does not exist on type '{1}'. Did you forget to use 'await'?": {
2016+
"category": "Error",
2017+
"code": 2570
2018+
},
20162019
"JSX element attributes type '{0}' may not be a union type.": {
20172020
"category": "Error",
20182021
"code": 2600
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/nonexistentPropertyAvailableOnPromisedType.ts(2,7): error TS2570: Property 'toLowerCase' does not exist on type 'Promise<string>'. Did you forget to use 'await'?
2+
3+
4+
==== tests/cases/compiler/nonexistentPropertyAvailableOnPromisedType.ts (1 errors) ====
5+
function f(x: Promise<string>) {
6+
x.toLowerCase();
7+
~~~~~~~~~~~
8+
!!! error TS2570: Property 'toLowerCase' does not exist on type 'Promise<string>'. Did you forget to use 'await'?
9+
}
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [nonexistentPropertyAvailableOnPromisedType.ts]
2+
function f(x: Promise<string>) {
3+
x.toLowerCase();
4+
}
5+
6+
7+
//// [nonexistentPropertyAvailableOnPromisedType.js]
8+
function f(x) {
9+
x.toLowerCase();
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/nonexistentPropertyAvailableOnPromisedType.ts ===
2+
function f(x: Promise<string>) {
3+
>f : Symbol(f, Decl(nonexistentPropertyAvailableOnPromisedType.ts, 0, 0))
4+
>x : Symbol(x, Decl(nonexistentPropertyAvailableOnPromisedType.ts, 0, 11))
5+
>Promise : Symbol(Promise, Decl(lib.d.ts, --, --))
6+
7+
x.toLowerCase();
8+
>x : Symbol(x, Decl(nonexistentPropertyAvailableOnPromisedType.ts, 0, 11))
9+
}
10+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/nonexistentPropertyAvailableOnPromisedType.ts ===
2+
function f(x: Promise<string>) {
3+
>f : (x: Promise<string>) => void
4+
>x : Promise<string>
5+
>Promise : Promise<T>
6+
7+
x.toLowerCase();
8+
>x.toLowerCase() : any
9+
>x.toLowerCase : any
10+
>x : Promise<string>
11+
>toLowerCase : any
12+
}
13+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/compiler/nonexistentPropertyOnUnion.ts(2,7): error TS2339: Property 'toLowerCase' does not exist on type 'string | Promise<string>'.
2+
Property 'toLowerCase' does not exist on type 'Promise<string>'.
3+
4+
5+
==== tests/cases/compiler/nonexistentPropertyOnUnion.ts (1 errors) ====
6+
function f(x: string | Promise<string>) {
7+
x.toLowerCase();
8+
~~~~~~~~~~~
9+
!!! error TS2339: Property 'toLowerCase' does not exist on type 'string | Promise<string>'.
10+
!!! error TS2339: Property 'toLowerCase' does not exist on type 'Promise<string>'.
11+
}
12+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [nonexistentPropertyOnUnion.ts]
2+
function f(x: string | Promise<string>) {
3+
x.toLowerCase();
4+
}
5+
6+
7+
//// [nonexistentPropertyOnUnion.js]
8+
function f(x) {
9+
x.toLowerCase();
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/nonexistentPropertyOnUnion.ts ===
2+
function f(x: string | Promise<string>) {
3+
>f : Symbol(f, Decl(nonexistentPropertyOnUnion.ts, 0, 0))
4+
>x : Symbol(x, Decl(nonexistentPropertyOnUnion.ts, 0, 11))
5+
>Promise : Symbol(Promise, Decl(lib.d.ts, --, --))
6+
7+
x.toLowerCase();
8+
>x : Symbol(x, Decl(nonexistentPropertyOnUnion.ts, 0, 11))
9+
}
10+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/nonexistentPropertyOnUnion.ts ===
2+
function f(x: string | Promise<string>) {
3+
>f : (x: string | Promise<string>) => void
4+
>x : string | Promise<string>
5+
>Promise : Promise<T>
6+
7+
x.toLowerCase();
8+
>x.toLowerCase() : any
9+
>x.toLowerCase : any
10+
>x : string | Promise<string>
11+
>toLowerCase : any
12+
}
13+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/nonexistentPropertyUnavailableOnPromisedType.ts(2,7): error TS2339: Property 'toLowerCase' does not exist on type 'Promise<number>'.
2+
3+
4+
==== tests/cases/compiler/nonexistentPropertyUnavailableOnPromisedType.ts (1 errors) ====
5+
function f(x: Promise<number>) {
6+
x.toLowerCase();
7+
~~~~~~~~~~~
8+
!!! error TS2339: Property 'toLowerCase' does not exist on type 'Promise<number>'.
9+
}
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [nonexistentPropertyUnavailableOnPromisedType.ts]
2+
function f(x: Promise<number>) {
3+
x.toLowerCase();
4+
}
5+
6+
7+
//// [nonexistentPropertyUnavailableOnPromisedType.js]
8+
function f(x) {
9+
x.toLowerCase();
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/nonexistentPropertyUnavailableOnPromisedType.ts ===
2+
function f(x: Promise<number>) {
3+
>f : Symbol(f, Decl(nonexistentPropertyUnavailableOnPromisedType.ts, 0, 0))
4+
>x : Symbol(x, Decl(nonexistentPropertyUnavailableOnPromisedType.ts, 0, 11))
5+
>Promise : Symbol(Promise, Decl(lib.d.ts, --, --))
6+
7+
x.toLowerCase();
8+
>x : Symbol(x, Decl(nonexistentPropertyUnavailableOnPromisedType.ts, 0, 11))
9+
}
10+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/nonexistentPropertyUnavailableOnPromisedType.ts ===
2+
function f(x: Promise<number>) {
3+
>f : (x: Promise<number>) => void
4+
>x : Promise<number>
5+
>Promise : Promise<T>
6+
7+
x.toLowerCase();
8+
>x.toLowerCase() : any
9+
>x.toLowerCase : any
10+
>x : Promise<number>
11+
>toLowerCase : any
12+
}
13+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function f(x: Promise<string>) {
2+
x.toLowerCase();
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function f(x: string | Promise<string>) {
2+
x.toLowerCase();
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function f(x: Promise<number>) {
2+
x.toLowerCase();
3+
}

0 commit comments

Comments
 (0)