Skip to content

this index with unions outputs intersections?Β #49393

Closed
@u128393

Description

@u128393

Bug Report

πŸ”Ž Search Terms

  • this index

πŸ•— Version & Regression Information

This changed between versions 3.3.3 and 3.5.1, and it keeps the behavior since then to the current latest version (at least 4.7.3).

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

class Test {
  a = '';
  b = 0;

  test() {
    const a: this['a'] = '';        // ok
    const b: this['b'] = 0;         // ok
    const c: Test['a' | 'b'] = '';  // ok
    const d: this['a' | 'b'] = '';  // fail
  }
}

πŸ™ Actual behavior

Type 'string' is not assignable to type 'this["a"] & this["b"]'.
  Type 'string' is not assignable to type 'this["b"]'.
    Type 'string' is not assignable to type 'number'.(2322)

πŸ™‚ Expected behavior

I expect this['a' | 'b'] to be string | number in this example, thus accepting the assignment of '' to it, just like Test['a' | 'b'].

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions