Skip to content

fix(64954): exclude PromiseLike from contextual this in object literal methods - #64963

Open
Nate (servusdei2018) wants to merge 2 commits into
microsoft:mainfrom
servusdei2018:bracyn/64954
Open

Nate (servusdei2018) wants to merge 2 commits into
microsoft:mainfrom
servusdei2018:bracyn/64954

Conversation

@servusdei2018

Copy link
Copy Markdown

When an async arrow function has a contextual return type, the object literal it returns is contextually typed as T | PromiseLike<T>. That union was then being used as this inside the literal’s methods, which is wrong, since this is the literal itself. This change strips the promise-like members from the union before typing this. If nothing is left after that, it falls back to the literal’s own type.

const f: () => Promise<{ value: number; method(): void }> = async () => {
  return {
    value: 5,
    method() {
      // was: this: { value: number; method(): void } | PromiseLike<...>
      // now: this: { value: number; method(): void }
      this;
    },
  };
};

Fixes #64954

@typescript-automation typescript-automation Bot added For Uncommitted Bug PR for untriaged, rejected, closed or missing bug For Backlog Bug PRs that fix a backlog bug and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Oct 25, 2026
@servusdei2018
Nate (servusdei2018) marked this pull request as ready for review October 26, 2026 22:01
Copilot AI balanced review requested due to automatic review settings October 26, 2026 22:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The unconditional filter also removes legitimate thenable contextual types outside async returns, causing missing-property errors in previously valid code.

1 open issue
What changed in this PR

Addresses #64954 by excluding promise-like contextual types from this in object literal methods, targeting objects returned from async functions.

Changes:

  • Filters promise-like contextual types, falling back to the literal’s own type when none remain.
  • Adds an async-return regression test with type, symbol, and JavaScript baselines.
File Description
tsc/​testdata/​tests/​cases/​compiler/​thisInObjectLiteralMethodAsyncReturn.ts Tests inferred and annotated async returns.
tsc/​testdata/​baselines/​reference/​compiler/​thisInObjectLiteralMethodAsyncReturn.types Records expected contextual this types.
tsc/​testdata/​baselines/​reference/​compiler/​thisInObjectLiteralMethodAsyncReturn.symbols Records expected symbol bindings.
tsc/​testdata/​baselines/​reference/​compiler/​thisInObjectLiteralMethodAsyncReturn.js Records emitted JavaScript.
tsc/​internal/​checker/​checker.go Filters promise-like types when determining contextual this.

Review effort: Balanced

Comment on lines +12293 to +12295
if contextualType != nil {
filtered := c.filterType(contextualType, func(t *Type) bool {
return c.getAwaitedTypeOfPromise(t) == nil

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type annotations cause contextual this in object literal returned from an async function to be inferred as Promise<This> | This

2 participants