Skip to content

Narrow "this" type #12587

Closed
Closed
@ghost

Description

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions