Skip to content

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

Closed
ghost opened this issue Nov 30, 2016 · 5 comments
Closed

Narrow "this" type #12587

ghost opened this issue Nov 30, 2016 · 5 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@ghost
Copy link

ghost commented Nov 30, 2016

TypeScript Version: nightly (2.2.0-dev.20161130)

Code

function f(this: string | number) {
    if (typeof this === "string") {
        const x: string = this;
    }
}

Expected behavior:

No error.

Actual behavior:

a.ts(3,15): error TS2322: Type 'string | number' is not assignable to type 'string'.

Rename this to me and it works.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Dec 5, 2016
@mhegazy mhegazy modified the milestones: sand, TypeScript 2.2 Dec 14, 2016
@mhegazy mhegazy modified the milestones: TypeScript 2.2, TypeScript 2.3 Feb 2, 2017
@mhegazy mhegazy modified the milestones: TypeScript 2.3, Future Mar 24, 2017
@yume-chan
Copy link
Contributor

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 this of instance methods, but not normal functions, it's so annoying.

@SalathielGenese
Copy link

SalathielGenese commented Dec 27, 2017

You are right @yume-chan , I even tried instanceof without success. Had to cast :

(<B>this).doB(); // now works.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Dec 27, 2017

FWIW I'm not really sure that narrowing this makes sense on a primitive. Won't this be Number | String if the LHS of the dot is a number | string? In other words, you'll always get "object".

function foo() { return this }
var x = foo.apply("hello");
console.log(typeof x);

@SalathielGenese
Copy link

@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;
}

@sandersn sandersn removed their assignment Jan 7, 2020
@Andarist
Copy link
Contributor

Andarist commented Jan 8, 2023

@RyanCavanaugh this one works OK today, the issue can be closed :)

@sandersn sandersn closed this as completed Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants