-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type information should be preserved with polymorphic this #23548
Comments
Duplicate of #10725? |
Not to mention it'd be flat-out broken if |
@RyanCavanaugh, I would assume that a function like |
@andy-ms, This suggestion is not particular to It seems intuitive to me that if a function is returning |
It doesn't seem to make sense for the |
This would be a large change, and would need its own proposal, but what do you think of a function that uses the Maybe an example would illustrate: class TestClass {
member = 5;
// current and proposed typechecker: fakeCopy passes checks
fakeCopy() {
return this;
}
// current and proposed typechecker: mutatingFunction passes checks (member is not marked readonly).
mutatingFunction() {
this.member = 6;
}
}
const tester: Readonly<TestClass> = new TestClass();
// current typechecker: function 'mutatingFunction' exists on typeof tester. Valid call.
// proposed typechecker: TestClass#mutatingFunction(this: typeof tester) fails because property member is readonly. Invalid call.
tester.mutatingFunction();
// current typechecker: tester.fakeCopy() returns a TestClass.
// proposed typechecker: tester.fakeCopy() passes typechecking because TestClass#fakeCopy(this: typeof tester) passes. Inferred return type is `typeof tester`
let copy = tester.fakeCopy(); This would fix both my problem and the need for a Thoughts? |
We've had lots of issues that boil down to "Implement C++-style |
I am closing this in favor of #23554 |
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:
In this example
fakeCopy
returns aTestClass
; it would be more intuitive iffakeCopy
would returntypeof tester
which would beReadonly<TestClass>
The text was updated successfully, but these errors were encountered: