Skip to content

TS2532 Compound null check, object cannot be undefined #35260

@oluwasayo

Description

@oluwasayo

TypeScript Version: 2.4.1-dev.20191120 (nightly)

TS2532
Object is possibly undefined
Narrow

Code

interface Version {
  createdAt: string
}

const stuff = (version?: Version, createdAt?: string) => {
  if (!(createdAt || version)) {
    return null;
  }

  return createdAt || version.createdAt;
};

Expected behavior:
version should be narrowed as defined.

Actual behavior:
version still considered possibly undefined.

(parameter) version: Version | undefined
Object is possibly 'undefined'.(2532)

Playground Link:
https://typescript-play.js.org/#code/JYOwLgpgTgZghgYwgAgGrQM7APYmQbwChlkEoI5IATAQTAC5kMwpQBzQgX0MIV2aZgArjBjIAvMgAUAN0w4QAfkbooWXABpS5ShFphlg1iDYBKCQD4CxZMDFSAhFLIVqdZAB8PyOWoWnzIhIScmEoPBAhABsogG4bbhtQoXDtVz13Lx95XAA6F119eM54oA

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions