-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Narrow "this" type #12587
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 {
doA() { }
tryDoB() {
if (isB(this)) {
this.doB(); // works
}
}
}
class B extends A {
doB() { }
}
declare function isB(value: any): value is B;
function tryDoB(this: A): void {
if (isB(this)) {
this.doB(); // Property 'doB' does not exist on type 'A'.
}
} Type guards work on |
You are right @yume-chan , I even tried (<B>this).doB(); // now works. |
FWIW I'm not really sure that narrowing function foo() { return this }
var x = foo.apply("hello");
console.log(typeof x); |
@DanielRosenwasser, Do you see anything worth it in the following code? interface A {}
interface B {}
interface C {}
function fn(this: boolean): A;
function fn(this: number): B;
function fn(this: string): C;
function fn(this: boolean | number | string): A | B | C
{
return <any>void 0;
} |
@RyanCavanaugh this one works OK today, the issue can be closed :) |
TypeScript Version: nightly (2.2.0-dev.20161130)
Code
Expected behavior:
No error.
Actual behavior:
a.ts(3,15): error TS2322: Type 'string | number' is not assignable to type 'string'.
Rename
this
tome
and it works.The text was updated successfully, but these errors were encountered: