Skip to content

Incorrect type inferred with early return  #19698

Closed
@Bibliofile

Description

@Bibliofile

When returning early from a function, types are not inferred correctly in functions defined later in the function. This occurs only with strictNullChecks turned on. Playground.

TypeScript Version: [email protected], [email protected]

Code

interface hasMethods {
    remove(): void
    getExports(key: string): {[key: string]: any} | undefined
}

let obj: hasMethods = {
    remove: () => { },
    getExports: () => undefined
}

;(function () {
    let x = obj.getExports('test')
    if (!x) return
    // No error, as expected
    x.test()
    // Error, x might be undefined
    obj.remove = () => x.test()
}())

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions