Skip to content

Type information should be preserved with polymorphic this #23548

@DavidANeil

Description

@DavidANeil

When the return type of a function is the this type, uses of that function should preserve the type information of the left hand operand.

See example:

class TestClass {
  constructor(public member: number) { }

  fakeCopy() {
    return this;
  }

  copy() {
    return new TestClass(this.member);
  }
}

const tester: Readonly<TestClass> = new TestClass(5);

tester.member = 6; // This properly errors.
tester.copy().member = 6; // This properly does not error.
tester.fakeCopy().member = 6; // This should error.

In this example fakeCopy returns a TestClass; it would be more intuitive if fakeCopy would return typeof tester which would be Readonly<TestClass>

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