📝If aliasing is possible, a function call invalidates value analysis / cached value

TypeScript does an analysis of variable’s possible value. For example:

function test(x: string | null) {
  if (x) {
    // x is known to be string here
  } else {
    // x is null | '' here
  }
}

Now, this doesn’t quite work with mutable cells and calls to functions:

function test(x: { y: string | null }) {
  if (x.y) {
    // x.y is known to be string here
    f();
    // x.y is string | null here because f could have altered it
  }
}

Backlinks

Want to receive my 🖋 posts as I publish them?