-
Notifications
You must be signed in to change notification settings - Fork 12.2k
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
Could there be a This
type like the Self
type in rust ?
#44055
Comments
Yes, interface Cloneable {
clone(): this
} |
This is not the desired behavior. |
Ah, apologies, I misread. |
Maybe somehow not. interface Foo {
bar(that: this): void
}
class A implements Foo {
a: number
constructor(a: number) {
this.a = a
}
bar(that: this) {
console.log(this.a + that.a)
}
}
const a1 = new A(1)
const a2 = new A(2)
a1.bar(a2) For me I would like |
@yw662 The fundamental problem with creating a This would require either something like constructor constraints for |
The only thing missing here is class A implements Cloneable {
readonly a: number
clone() {
return new A(this.a) as this;
}
constructor(a: number) {
this.a = a
}
} which isn't really a big hoop to jump through - it's just a disavowal of the existence of nonconforming derived classes. @MartinJohns 's comments are apt. A derived class with no further implementation is assumed to be substitutable for its base class and breaking that assumption is a sort of big leap that requires new constraints. |
A type like this would allow for an easy way of implementing immutable classes based on an abstract class (or just using inheritance at all really) as each methods "altering the state" should return a new instance of said class. |
Suggestion
π Search Terms
This, Self, typeof this
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
For example, let's declare an interface
Cloneable
, instances of classes implementingCloneable
should be able to clone themselves:The return type of
function clone()
ofinterface Cloneable
should be the actual type of this, ortypeof this
in the shown code. Howevertypeof this
does not work. It will raise TS2304Cannot find name 'this'
, and TS4055Return type of public method from exported class has or is using private name 'this'
.clone(): Cloneable
is not the desired way for this interface, or the below code would pass type check:Besides,
clone()
is not returning this. Soclone(): this
is not the right way to go. It won't allow this to pass:Generics could be a workaround, but it can be annoying for complex cases, and still can be a problem:
It does not make any sense to have sth like
class A implements Cloneable<B>
, but generics allows it.Allowing
typeof this
, orThis
, could be a very useful in that case.This feature could also help in some other cases, like
interface Addable
:The argument
that
of functionadd
must be of the same type ofthis
, so it cannot be declared asadd(that: Addable): Addable
. It also cannot beadd(that: this): this
.It makes sense to use generics,
But still, it is not a very good way.
π Motivating Example
π» Use Cases
As discussed above, it is mainly used by interfaces and abstract classes, like
interface Cloneable
,interface Addable
, or maybeinterface Serializable
andinterface Serializer
.The current approaches cannot provide the correct behavior, which is discussed above.
I am using generics, but it is incorrect indeed.
The text was updated successfully, but these errors were encountered: