-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type 'this'??? #9063
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
Comments
class A {
me(): this {
return this;
}
} |
So I tried that and it started to unravel when I got into sub classes... |
What do you mean by "current type"; and what happened with subclasses when you used this? |
Yes. class A {
me():this {
return this;
}
}
class B extends A {
otherThing():void{}
me():this {
return this;
}
}
let a = new A, b= new B();
let foo = a.me(); // foo:A
let bar = b.me(); // bar:B You can hover over |
Also, you don't need to override it i.e. the base case class A {
me():this {
return this;
}
}
class B extends A {
otherThing():void{}
}
let a = new A, b= new B();
let foo = a.me(); // foo:A
let bar = b.me(); // bar:B |
also, class A {
me() {
return this; // infers type this
}
} |
Hey thanks guys! I'm gonna check this out. It may make a huge difference in some of my class structures. I did try and do this out of instinct, but it didn't work right on my first try. I'll get back to you. ;) |
Ok so here's where it get's sticky... class A<T> {
me():this {
return this;
}
}
class B<T> extends A<T> {
otherThing():void{}
}
class C<T> extends B<T> {
map<TResult>(t:(v:T)=>TResult):this<TResult> {}
} Anything that will make this work? :/ |
Minor simplification of your code (that still errors): class A<T> {
map<TResult>(mapper: (v: T) => TResult): this<TResult>{
}
} However |
Yes, you get it. So lastly is there any consequences to doing something like: class A<T> {
clone():this {
return new A<T>();
}
} |
If a child class |
Ok so assuming that it the type returned always matches correctly, then ok right? class A<T> {
clone():this {
return <any> new A<T>(); // without <any> it complains.
}
me():this {
return this; // doesn't complain
}
} |
So I got everything to compile. |
Is there any magic (without insane generics) to define a type that matches 'this'?
For example...
Instead of overriding
me()
can I define a type without adding another generic declaration that simply solves the type ofme()
?The text was updated successfully, but these errors were encountered: