Skip to content

Type guard question #1892

Closed
Closed
@jbondc

Description

@jbondc
module foo {
    export function on(event: string|Event) {
        if (typeof event === "string") {
            event = new Event(event) // (a)
            event = new Event(String(event)) // (b)
        }

        console.log(event.target) 
    }

    class Event {
        constructor(name: string) {}
    }
}

Is this a bug, known issue?

Using the latest tsc from master, get the errors:
(a) error TS2345: Argument of type 'string | Event' is not assignable to parameter of type 'string'.
(b) error TS2339: Property 'target' does not exist on type 'string | Event'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions