Skip to content

Validation of variable is not undefined doesn't work if it's not done directly inside if statementΒ #52429

Closed
@eliezra236

Description

@eliezra236

Bug Report

πŸ”Ž Search Terms

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

const map = new Map<string, number>();
map.set('a', 1);
const a = map.get('a');

const isAValid = Boolean(a);

if (!isAValid) {
    throw Error('no a');
}

const aCopy: number = a;

if (!a) {
    throw Error('no a')
}

const aCopy2: number = a;

πŸ™ Actual behavior

In the first example aCopy gives error because it can be undefined according to typescript

πŸ™‚ Expected behavior

aCopy should be defined to be a number.

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