Microsoft/TypeScript

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

Open

#56.024 aperta il 8 ott 2023

Vedi su GitHub
 (5 commenti) (0 reazioni) (0 assegnatari)TypeScript (6726 fork)batch import
BugDomain: This-TypingHelp Wanted

Metriche repository

Star
 (48.455 star)
Metriche merge PR
 (Merge medio 6g 17h) (9 PR mergiate in 30 g)

Descrizione

🔎 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
        }
    }
});

Guida contributor