Skip to content

Incorrect type inferred with early return  #19698

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: typescript@2.6, typescript@2.7.0-dev.20171102

Code

interfacehasMethods{remove(): voidgetExports(key: string): {[key: string]: any}|undefined}letobj: hasMethods={remove: ()=>{},getExports: ()=>undefined};(function(){letx=obj.getExports('test')if(!x)return// No error, as expectedx.test()// Error, x might be undefinedobj.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