Skip to content

CFA does not consider pass-by-reference to invalidate assumptionsΒ #61416

@Sainan

Description

@Sainan

πŸ”Ž Search Terms

control flow analysis, branch, condition

πŸ•— Version & Regression Information

Tested in 5.8.2.

⏯ Playground Link

https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true#code/JYOwLgpgTgZghgYwgAgBIFcC2cTIN4BQyycA5hAPwBcyIWARtANwEC+BBM6ICYwA9rgSCAzlggAKfvQBWNDNhABKfEWTSZAOjIoAvMm4ATCDFARDLdgWEgRYdbPlYcyfXhLkaAFgBMyVizAMMhSstrkKoTENmKYkhpKLNGi9jo0dJiMUK4OWjpMyAD0hcgAcvypuNBQ-FBsBEA

πŸ’» Code

interface Human {
  age?: number;
}

function consume(obj: Human) {
  obj.age = undefined;
}

const obj: Human = { age: 42 };
if (obj.age) {
  consume(obj);
  const age: number = obj.age; // Not an error
}

πŸ™ Actual behavior

Assignment of const age: number succeeds with undefined.

πŸ™‚ Expected behavior

obj.age is assessed as number | undefined, causing the assignment to fail.

Additional information about the issue

No response

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