Skip to content
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

Fixed a false positive "await has no effect on the type" diagnostic with mixed generic union #50833

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Expand Up @@ -36923,7 +36923,7 @@ namespace ts {
// We only need `Awaited<T>` if `T` is a type variable that has no base constraint, or the base constraint of `T` is `any`, `unknown`, `{}`, `object`,
// or is promise-like.
if (baseConstraint ?
baseConstraint.flags & TypeFlags.AnyOrUnknown || isEmptyObjectType(baseConstraint) || isThenableType(baseConstraint) :
baseConstraint.flags & TypeFlags.AnyOrUnknown || isEmptyObjectType(baseConstraint) || someType(baseConstraint, isThenableType) :
Copy link
Contributor Author

@Andarist Andarist Sep 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative fix would be to move someType to isThenableType:

patch
diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts
index 088ba325ec..89cfb6bcae 100644
--- a/src/compiler/checker.ts
+++ b/src/compiler/checker.ts
@@ -36884,8 +36884,10 @@ namespace ts {
                 return false;
             }
 
-            const thenFunction = getTypeOfPropertyOfType(type, "then" as __String);
-            return !!thenFunction && getSignaturesOfType(getTypeWithFacts(thenFunction, TypeFacts.NEUndefinedOrNull), SignatureKind.Call).length > 0;
+            return someType(type, t => {
+                const thenFunction = getTypeOfPropertyOfType(t, "then" as __String);
+                return !!thenFunction && getSignaturesOfType(getTypeWithFacts(thenFunction, TypeFacts.NEUndefinedOrNull), SignatureKind.Call).length > 0;
+            });
         }
 
         interface AwaitedTypeInstantiation extends Type {
@@ -36923,7 +36925,7 @@ namespace ts {
                 // We only need `Awaited<T>` if `T` is a type variable that has no base constraint, or the base constraint of `T` is `any`, `unknown`, `{}`, `object`,
                 // or is promise-like.
                 if (baseConstraint ?
-                    baseConstraint.flags & TypeFlags.AnyOrUnknown || isEmptyObjectType(baseConstraint) || someType(baseConstraint, isThenableType) :
+                    baseConstraint.flags & TypeFlags.AnyOrUnknown || isEmptyObjectType(baseConstraint) || isThenableType(baseConstraint) :
                     maybeTypeOfKind(type, TypeFlags.TypeVariable)) {
                     return true;
                 }

That would in turn impact the other call site to this util, here, and I'm not sure if that's desirable.

maybeTypeOfKind(type, TypeFlags.TypeVariable)) {
return true;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/cases/fourslash/codeFixRemoveUnnecessaryAwait_mixedUnion.ts
@@ -0,0 +1,12 @@
/// <reference path="fourslash.ts" />

// @target: esnext
//// async function fn1(a: Promise<void> | void) {
//// await a;
//// }
////
//// async function fn2<T extends Promise<void> | void>(a: T) {
//// await a;
//// }

verify.getSuggestionDiagnostics([]);