- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.1k
Description
Bug Report
🔎 Search Terms
"this" key typing, conditional types on "this"
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about this
⏯ Playground Link
Playground link with relevant code
💻 Code
class Test {
  public key1: string = "abc";
  public doSomethingThis(): void {
    const wrongVariableOfTypeKey1: this["key1"] = 5; // ✔This should be a typing error and it is
    const correctVariableOfTypeKey1: this["key1"] = "string"; // ✔ This should not be a typing error and it's not
    type ConditionalThisType = this["key1"] extends string ? number : string;
    const a: ConditionalThisType = "string"; // ✔This should be a typing error and it is
    const b: ConditionalThisType = 5; // ❌This should NOT be a typing error BUT it is
    console.log(a, b);
  }
  public doSomethingThisClass(): void {
    type ConditionalClassType = Test["key1"] extends string ? number : string;
    const a: ConditionalClassType = "string"; // ✔ This should be a typing error and it is
    const b: ConditionalClassType = 5; // ✔ This should not be a typing error and it's not
    console.log(a, b);
  }
}🙁 Actual behavior
this["key1"] extends string is neither true nor false and no path of the conditional type is returned. It's not possible to assign either a string or a number to a variable with the type.
🙂 Expected behavior
this["key1"] extends string should be true in the example above and therefore the first part of the conditional typing should get returned and the ConditionalThisType should be number. In all cases, it should be possible to assign at least either a string or a number because the documentation for conditional types states that either the first branch or the second branch will be returned:
When the type on the left of the extends is assignable to the one on the right, then you’ll get the type in the first branch (the “true” branch); otherwise you’ll get the type in the latter branch (the “false” branch).