Skip to content

object is possibly undefined even though check is done #34535

Closed
@barroudjo

Description

@barroudjo

TypeScript Version: 3.6.3

Search Terms: object is possibly undefined

Code

const foo = (bar: { baz?: { name: string } }) => {
    const isBarBazDefined = !!bar.baz;
    if(isBarBazDefined) console.log(bar.baz.name);
}

Expected behavior:
Typescript should not give an 'object is possibly undefined' error.

Actual behavior:
Typescript does give an 'object is possibly undefined' error.

Playground Link:
http://www.typescriptlang.org/play/?ssl=4&ssc=2&pln=1&pc=1#code/MYewdgzgLgBAZiEMC8MAUAjAhgJwFwwDeM2AXgPwHFhYC2ApgdDgJZgDmMAvtwJQoA+IgCgYYmKEiwWEAEK55pACL04begBMUMAIQ7sOAHRkA3KPEs4aGfJyKVasJv6SIIADb1D7kO0y5jLFJDGgZeMy4gA

Related Issues:
Couldn't find any.

I realise this is very simple case, but I haven't been able to find an existing issue or FAQ entry on the subject...

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