Skip to content

functions returning never are not treated the same as raw throw calls in control-flow analysis #9655

Closed
@rkirov

Description

@rkirov

TypeScript Version: 2.0.0-beta

Code

function fail(): never {
  throw 'error';
}

enum E {
  A,
  B
}
function f(t: E) {
  let s: string;
  switch (t) {
    case E.A:
      s = 'a';
      break;
    case E.B:
      s = 'b';
      break;
    default:
      fail();  // but replacing with "throw 'error"' removes the TS error.
  }
  s += '';
}

Expected behavior:
s += '' is allowed because all terminating (non-throwing) branches assign to s.

In general, I am working under the assumption that calling functions that returning never are equivalent to raw throw calls during control-flow analysis.

Actual behavior:
ERROR(21,3): : Variable 's' is used before being assigned.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions