Skip to content

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

Open

Description

🔎 Search Terms

"this" "Promise" "async" "function" "inferred" "inference" "type" "annotations" "return" "object" "literal" "method"

🕗 Version & Regression Information

This bug exists down to typescript 3.3.3333, which is the earliest version available in the playground.
(I think the playground wrongly displays 3.3.3 version as 3.3.3333)

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.3#code/MYewdgzgLgBFCm0CMMC8MCGECeZgwAoBKNAPhgG8AoGGAJ3igFc6xKbaYA3DAGyfgAuGAFYANB1oBbRgAsQAE2LtOnKLICWEGBAxQtAMw2JK3PgOFgmUgEbw6YmDPWLiwriA0KYAXwDcMJK+ErT+VD5UVKCQsAjQAEzCyqjkAAp0IFJa8AA81Ko8-EIwVrb2fkHO8kpE7p4KFT7k6Fi4+Mnk+fSMLGxdtIUWoiGcVa4k-ZwA9FMwAAJQEAC08AAeAA7wwFArdBl0wgAq2JswAOQUZkWW1nZ0AWM1dV4BPjAAPjDpmdkAMhoAa1yl0GxVKdwecnGzwavlIZxgChAJjAIFgun0EAM2DgsngMDWm228G8UBO+IuVyG4PKTihT249VeZwAdEFaOotDo9IZjNoQeYwbd7I5Hm5GV5fAEgj4RmEIkA

💻 Code

const test1 = async () => {
  return {
    value: 5,
    method() {
      this satisfies { value: number, method(): void }; 
    },
  };
}

const test2: () => Promise<{
    value: number;
    method(): void;
}> = async () => {
  return {
    value: 5,
    method() {
      // @ts-expect-error: Type '{ value: number; method(): void; } | PromiseLike<{ value: number; method(): void; }>' does not satisfy the expected type '{ value: number; method(): void; }'.
      this satisfies { value: number, method(): void }; 
    },
  };
}

🙁 Actual behavior

Actual this type: { value: number; method(): void; } | PromiseLike<{ value: number; method(): void; }>

🙂 Expected behavior

Expected this type: { value: number; method(): void }

Additional information about the issue

This probably a duplicate as it is a very old bug, but I couldn't find an existing issue.

Typescript lsp shows that type of a test1 is:

const test1: () => Promise<{
    value: number;
    method(): void;
}>

So types of test1 and test2 are exactly the same, but the contextual type information causes the incorrect type to be inferred.

in the real code this happens more often in async callbacks.

satisfies in the example is to just throw an error, so that ts-expect-error can be used.

Activity

  1. changed the title [-]Type annotations cause contextual `this` in object literal returned from an async function type as Promise<This> | This[/-] [+]Type annotations cause contextual `this` in object literal returned from an async function being inferred as `Promise<This> | This`[/+] on Oct 25, 2026
  2. changed the title [-]Type annotations cause contextual `this` in object literal returned from an async function being inferred as `Promise<This> | This`[/-] [+]Type annotations cause contextual `this` in object literal returned from an async function to be inferred as `Promise<This> | This`[/+] on Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions