Skip to content

Commit d789ce0

Browse files
committed
Fix issue microsoft#22923
1 parent 9b558f9 commit d789ce0

17 files changed

+267
-1
lines changed

src/compiler/checker.ts

+9
Original file line numberDiff line numberDiff line change
@@ -16409,6 +16409,9 @@ namespace ts {
1640916409
if (suggestion !== undefined) {
1641016410
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2, declarationNameToString(propNode), typeToString(containingType), suggestion);
1641116411
}
16412+
else if (isPromiseLike(containingType)) {
16413+
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_forget_to_await_the_1, declarationNameToString(propNode), typeToString(containingType));
16414+
}
1641216415
else {
1641316416
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1, declarationNameToString(propNode), typeToString(containingType));
1641416417
}
@@ -16420,6 +16423,12 @@ namespace ts {
1642016423
return suggestion && symbolName(suggestion);
1642116424
}
1642216425

16426+
function isPromiseLike(type: Type, reportErrors = false): boolean {
16427+
const types: Type[] = (type.flags & TypeFlags.UnionOrIntersection)
16428+
? (type as UnionOrIntersectionType).types : [type];
16429+
return types.some(t => isReferenceToType(t, getGlobalPromiseType(reportErrors)));
16430+
}
16431+
1642316432
function getSuggestionForNonexistentSymbol(location: Node, outerName: __String, meaning: SymbolFlags): string {
1642416433
Debug.assert(outerName !== undefined, "outername should always be defined");
1642516434
const result = resolveNameHelper(location, outerName, meaning, /*nameNotFoundMessage*/ undefined, outerName, /*isUse*/ false, /*excludeGlobals*/ false, (symbols, name, meaning) => {

src/compiler/diagnosticMessages.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -2000,7 +2000,10 @@
20002000
"category": "Error",
20012001
"code": 2569
20022002
},
2003-
2003+
"Property '{0}' does not exist on type '{1}'. Did you forget to await the '{1}'?": {
2004+
"category": "Error",
2005+
"code": 2570
2006+
},
20042007
"JSX element attributes type '{0}' may not be a union type.": {
20052008
"category": "Error",
20062009
"code": 2600
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/missingPropertyOfPromise.ts(2,7): error TS2570: Property 'toLowerCase' does not exist on type 'Promise<string>'. Did you forget to await the 'Promise<string>'?
2+
3+
4+
==== tests/cases/compiler/missingPropertyOfPromise.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 await the 'Promise<string>'?
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [missingPropertyOfPromise.ts]
2+
function f(x: Promise<string>) {
3+
x.toLowerCase();
4+
}
5+
6+
7+
//// [missingPropertyOfPromise.js]
8+
function f(x) {
9+
x.toLowerCase();
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/missingPropertyOfPromise.ts ===
2+
function f(x: Promise<string>) {
3+
>f : Symbol(f, Decl(missingPropertyOfPromise.ts, 0, 0))
4+
>x : Symbol(x, Decl(missingPropertyOfPromise.ts, 0, 11))
5+
>Promise : Symbol(Promise, Decl(lib.d.ts, --, --))
6+
7+
x.toLowerCase();
8+
>x : Symbol(x, Decl(missingPropertyOfPromise.ts, 0, 11))
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/missingPropertyOfPromise.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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
tests/cases/compiler/missingPropertyOfPromiseIntersection.ts(10,7): error TS2570: Property 'method' does not exist on type 'Promise<Foo> & Bar'. Did you forget to await the 'Promise<Foo> & Bar'?
2+
3+
4+
==== tests/cases/compiler/missingPropertyOfPromiseIntersection.ts (1 errors) ====
5+
interface Foo {
6+
method();
7+
}
8+
9+
interface Bar {
10+
somethingElse();
11+
}
12+
13+
function f(x: Promise<Foo> & Bar) {
14+
x.method();
15+
~~~~~~
16+
!!! error TS2570: Property 'method' does not exist on type 'Promise<Foo> & Bar'. Did you forget to await the 'Promise<Foo> & Bar'?
17+
}
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [missingPropertyOfPromiseIntersection.ts]
2+
interface Foo {
3+
method();
4+
}
5+
6+
interface Bar {
7+
somethingElse();
8+
}
9+
10+
function f(x: Promise<Foo> & Bar) {
11+
x.method();
12+
}
13+
14+
15+
//// [missingPropertyOfPromiseIntersection.js]
16+
function f(x) {
17+
x.method();
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/compiler/missingPropertyOfPromiseIntersection.ts ===
2+
interface Foo {
3+
>Foo : Symbol(Foo, Decl(missingPropertyOfPromiseIntersection.ts, 0, 0))
4+
5+
method();
6+
>method : Symbol(Foo.method, Decl(missingPropertyOfPromiseIntersection.ts, 0, 15))
7+
}
8+
9+
interface Bar {
10+
>Bar : Symbol(Bar, Decl(missingPropertyOfPromiseIntersection.ts, 2, 1))
11+
12+
somethingElse();
13+
>somethingElse : Symbol(Bar.somethingElse, Decl(missingPropertyOfPromiseIntersection.ts, 4, 15))
14+
}
15+
16+
function f(x: Promise<Foo> & Bar) {
17+
>f : Symbol(f, Decl(missingPropertyOfPromiseIntersection.ts, 6, 1))
18+
>x : Symbol(x, Decl(missingPropertyOfPromiseIntersection.ts, 8, 11))
19+
>Promise : Symbol(Promise, Decl(lib.d.ts, --, --))
20+
>Foo : Symbol(Foo, Decl(missingPropertyOfPromiseIntersection.ts, 0, 0))
21+
>Bar : Symbol(Bar, Decl(missingPropertyOfPromiseIntersection.ts, 2, 1))
22+
23+
x.method();
24+
>x : Symbol(x, Decl(missingPropertyOfPromiseIntersection.ts, 8, 11))
25+
}
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/missingPropertyOfPromiseIntersection.ts ===
2+
interface Foo {
3+
>Foo : Foo
4+
5+
method();
6+
>method : () => any
7+
}
8+
9+
interface Bar {
10+
>Bar : Bar
11+
12+
somethingElse();
13+
>somethingElse : () => any
14+
}
15+
16+
function f(x: Promise<Foo> & Bar) {
17+
>f : (x: Promise<Foo> & Bar) => void
18+
>x : Promise<Foo> & Bar
19+
>Promise : Promise<T>
20+
>Foo : Foo
21+
>Bar : Bar
22+
23+
x.method();
24+
>x.method() : any
25+
>x.method : any
26+
>x : Promise<Foo> & Bar
27+
>method : any
28+
}
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
tests/cases/compiler/missingPropertyOfPromiseUnion.ts(10,7): error TS2570: Property 'method' does not exist on type 'Promise<Foo> | Promise<Bar>'. Did you forget to await the 'Promise<Foo> | Promise<Bar>'?
2+
Property 'method' does not exist on type 'Promise<Foo>'.
3+
4+
5+
==== tests/cases/compiler/missingPropertyOfPromiseUnion.ts (1 errors) ====
6+
interface Foo {
7+
method();
8+
}
9+
10+
interface Bar {
11+
somethingElse();
12+
}
13+
14+
function f(x: Promise<Foo> | Promise<Bar>) {
15+
x.method();
16+
~~~~~~
17+
!!! error TS2570: Property 'method' does not exist on type 'Promise<Foo> | Promise<Bar>'. Did you forget to await the 'Promise<Foo> | Promise<Bar>'?
18+
!!! error TS2570: Property 'method' does not exist on type 'Promise<Foo>'.
19+
}
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [missingPropertyOfPromiseUnion.ts]
2+
interface Foo {
3+
method();
4+
}
5+
6+
interface Bar {
7+
somethingElse();
8+
}
9+
10+
function f(x: Promise<Foo> | Promise<Bar>) {
11+
x.method();
12+
}
13+
14+
15+
//// [missingPropertyOfPromiseUnion.js]
16+
function f(x) {
17+
x.method();
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
=== tests/cases/compiler/missingPropertyOfPromiseUnion.ts ===
2+
interface Foo {
3+
>Foo : Symbol(Foo, Decl(missingPropertyOfPromiseUnion.ts, 0, 0))
4+
5+
method();
6+
>method : Symbol(Foo.method, Decl(missingPropertyOfPromiseUnion.ts, 0, 15))
7+
}
8+
9+
interface Bar {
10+
>Bar : Symbol(Bar, Decl(missingPropertyOfPromiseUnion.ts, 2, 1))
11+
12+
somethingElse();
13+
>somethingElse : Symbol(Bar.somethingElse, Decl(missingPropertyOfPromiseUnion.ts, 4, 15))
14+
}
15+
16+
function f(x: Promise<Foo> | Promise<Bar>) {
17+
>f : Symbol(f, Decl(missingPropertyOfPromiseUnion.ts, 6, 1))
18+
>x : Symbol(x, Decl(missingPropertyOfPromiseUnion.ts, 8, 11))
19+
>Promise : Symbol(Promise, Decl(lib.d.ts, --, --))
20+
>Foo : Symbol(Foo, Decl(missingPropertyOfPromiseUnion.ts, 0, 0))
21+
>Promise : Symbol(Promise, Decl(lib.d.ts, --, --))
22+
>Bar : Symbol(Bar, Decl(missingPropertyOfPromiseUnion.ts, 2, 1))
23+
24+
x.method();
25+
>x : Symbol(x, Decl(missingPropertyOfPromiseUnion.ts, 8, 11))
26+
}
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/compiler/missingPropertyOfPromiseUnion.ts ===
2+
interface Foo {
3+
>Foo : Foo
4+
5+
method();
6+
>method : () => any
7+
}
8+
9+
interface Bar {
10+
>Bar : Bar
11+
12+
somethingElse();
13+
>somethingElse : () => any
14+
}
15+
16+
function f(x: Promise<Foo> | Promise<Bar>) {
17+
>f : (x: Promise<Foo> | Promise<Bar>) => void
18+
>x : Promise<Foo> | Promise<Bar>
19+
>Promise : Promise<T>
20+
>Foo : Foo
21+
>Promise : Promise<T>
22+
>Bar : Bar
23+
24+
x.method();
25+
>x.method() : any
26+
>x.method : any
27+
>x : Promise<Foo> | Promise<Bar>
28+
>method : any
29+
}
30+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function f(x: Promise<string>) {
2+
x.toLowerCase();
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
interface Foo {
2+
method();
3+
}
4+
5+
interface Bar {
6+
somethingElse();
7+
}
8+
9+
function f(x: Promise<Foo> & Bar) {
10+
x.method();
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
interface Foo {
2+
method();
3+
}
4+
5+
interface Bar {
6+
somethingElse();
7+
}
8+
9+
function f(x: Promise<Foo> | Promise<Bar>) {
10+
x.method();
11+
}

0 commit comments

Comments
 (0)