Microsoft/TypeScript

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

Open

#56.024 geöffnet am 8. Okt. 2023

Auf GitHub ansehen
 (5 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
BugDomain: This-TypingHelp Wanted

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

Beschreibung

🔎 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