-
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 'Scope' is not assignable to type 'this' #8164
Comments
If an explicit type annotation is added, the error goes away: ...
find(name: string) {
var scope: Scope = this; // NB: type annotation added here
... ...but that type annotation seems superfluous given that |
I maybe wrong, but type The problem really seems to be is you can't pass class Scope {
private parent: this;
constructor(private locals: string[], parent: any) {
this.parent = parent;
}
find(name: string) {
var scope = this;
while (scope && scope.locals.indexOf(name) === -1) {
scope = scope.parent;
}
return scope;
}
} |
@kitsonk thanks for the input
I don't suppose you could come up with a little example of this? I'm not really understanding the type error. Especially since the code works and is reasonably idiomatic as JavaScript, I would have thought. |
Since 1.7, in classes and interfaces, there is a special class Animal {
walk() {
console.log('walking...');
return this;
}
}
class Dog extends Animal {
bark() {
console.log('barking...');
return this;
}
}
const dog = new Dog();
dog
.walk() // < 1.7 would have returned type Animal
.bark(); // so you wouldn't have been able to do this So something of type |
But even if the instance was a subclass of |
Yes, that is why type |
The obvious thing to try next is to change the constructor annotations to: constructor(private locals: string[], private parent: this) {
} ...but that produces another error: Then I tried (similar to your first suggestion): class Scope {
private parent: this;
constructor(private locals: string[], parent: Scope) {
this.parent = parent; // ERROR: Type 'Scope' is not assignable to type this
} In the end I've just added a type assertion on the EDIT: I should add that |
Yes, I ran into the same issues you did in "fixing" it, in that you cannot type guard I think though the |
as @kitsonk explained, |
@mhegazy why is it not allowed to do the following (the first thing I tried to fix the
That gives the error |
issue #5863 tracks adding it. Polymorphic this types are an instance type feature, the constructor is part of the static side of the class. |
Thanks! |
TypeScript Version:
1.9.0-dev.20160419
1.8.7
Code
Expected behavior:
Actual behavior:
The text was updated successfully, but these errors were encountered: