Skip to content

Type guards in string literals in callbacks #9774

Closed
@normalser

Description

@normalser

TypeScript Version: (2.0.0-beta)

Code

interface Test1 {
    type: 'test1'
    label: 'string'
}
interface Test2 {
    type: 'test2'
}

let a: Test1 | Test2

if (a.type == 'test1') {
    console.log(a.label) // works ok
    setTimeout(()=>{
        console.log(a.label) // Error: Property 'label' does not exist on type 'Test1 | Test2'
    })
}

Expected behavior:
setTimeout case should work ?

Actual behavior:
Error: Property 'label' does not exist on type 'Test1 | Test2' in setTimeout

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions