Microsoft/TypeScript

`this` parameter not correctly inferred when unrelated type parameter has no inference candidates

Open

#56,024 opened on Oct 8, 2023

View on GitHub
 (5 comments) (0 reactions) (0 assignees)TypeScript (6,726 forks)batch import
BugDomain: This-TypingHelp Wanted

Repository metrics

Stars
 (48,455 stars)
PR merge metrics
 (Avg merge 6d 17h) (9 merged PRs in 30d)

Description

🔎 Search Terms

generics bug

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

No response

💻 Code

function test<
  Data1 extends Record<string, number>,
  Data2 extends Record<string, ()=>number>,
  Func extends Record<string, (this: Data2)=>void>,
>(options: {
  data1?: Data1,
  data2: Data2,
  func: Func,
}) {
    console.log(options);
};

test({
    data1: {
        a1: 1
    },
    data2: {
        a2 () {return 2;}
    },
    func: {
        f1 () {
            this.a2; // this.a2 has no type hints
        }
    }
});

🙁 Actual behavior

this.a2 has no type hints

🙂 Expected behavior

this.a2 has type hints

Additional information about the issue

If you comment out data1, then type hints appear

function test<
  Data1 extends Record<string, number>,
  Data2 extends Record<string, ()=>number>,
  Func extends Record<string, (this: Data2)=>void>,
>(options: {
  data1?: Data1,
  data2: Data2,
  func: Func,
}) {
    console.log(options);
};

test({
    // data1: {
    //     a1: 1
    // },
    data2: {
        a2 () {return 2;}
    },
    func: {
        f1 () {
            this.a2; // If you comment out data1, then type hints appear
        }
    }
});

Contributor guide