Repository navigation
Fix this type in object literal methods returned from async functions - #64962
Open
Harsh Vardhan Chauhan (1-DARK) wants to merge 1 commit into
Open
Harsh Vardhan Chauhan (1-DARK) wants to merge 1 commit into
Harsh Vardhan Chauhan (1-DARK) wants to merge 1 commit into
Conversation
The contextual type of an async return expression is T | PromiseLike<T>. Exclude the synthesized PromiseLike<T> when deriving 'this'.
Harsh Vardhan Chauhan (1-DARK)
force-pushed
the
fix-async-return-this-type
branch
from
October 25, 2026 19:35
3def6cb to
7787536
Compare
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The filter mishandles returned thenables and union-valued awaited types, and regression tests are not included.
3 open issues
What changed in this PR
This PR addresses #64954 by adjusting contextual this typing for object-literal methods returned directly from async functions in the native TypeScript compiler.
Changes:
- Filters matching synthesized
PromiseLike<T>members before derivingthis. - Adds detection for direct async returns, including parenthesized expressions and concise arrow bodies, while excluding generators.
| File | Description |
|---|---|
tsc/internal/checker/checker.go |
Adds async-return detection and contextual this filtering. |
Review effort: Balanced
Comment on lines
+12291
to
+12292
| if contextualType.flags&TypeFlagsUnion != 0 && c.isAsyncReturnExpression(containingLiteral) { | ||
| // The contextual type of an async return expression is `T | PromiseLike<T>`; `this` is just `T`. |
Comment on lines
+12300
to
+12301
| args := c.getTypeArguments(t) | ||
| return len(args) != 1 || !slices.Contains(members, args[0]) |
| // for 'this' is the non-null form of the contextual type for the containing object literal or | ||
| // the type of the object literal itself. | ||
| if contextualType != nil { | ||
| if contextualType.flags&TypeFlagsUnion != 0 && c.isAsyncReturnExpression(containingLiteral) { |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Fixes #64954
In an async function, the contextual type of a returned expression is
T | PromiseLike<T>.getContextualThisParameterTypeused that whole union asthisfor methods in a returned object literal. Sothiswas{…} | PromiseLike<{…}>instead of the object type, andthis satisfies { … }failed with TS1360.This only happened when the return type came from context, such as an annotated variable or an async callback. The unannotated form infers the literal's own type, so it was already correct.
Fix
When the containing object literal is directly returned from an async, non-generator function, drop the synthesized
PromiseLike<U>from the contextual type before derivingthis.returnstatement or an arrow body.PromiseLike<U>whoseUis also a member of the same union.PromiseLikethat isn't tied to a sibling member is left alone.Verification
foo(async () => ({…}))returnasync functionexpression callbackasync function f(): Promise<X>{ inner: {…} })Promise<X[]>)The full
go test ./...suite passes with no baseline changes.Notes
asyncReturnObjectLiteralThisType.ts, plus baselines) was written but isn't part of this PR.