Skip to content

Treat expr satisfies never; similarly to a call to a never-returning function in CFAΒ #61004

@uhyo

Description

@uhyo

πŸ” Search Terms

Control flow analysys, CFA, satisfies, never, return, termination, assert

βœ… Viability Checklist

⭐ Suggestion

In CFA, expr satisfies never; statements should be treated similarly to a call to a never-returning function, treating statements that follow as unreachable.

I'm suggesting that TS enhance the expr satisfies never; pattern which is one of the ways to do the exhaustiveness check. It is a very popular technique used when dealing with discriminated union types. The ways often used are:

// 1-a. assign to a never-typed variable and abort
const _exhaustivenessCheck: never = option;
throw new Error("unreachable");

// 1-b. return never instead of abort
const _exhaustivenessCheck: never = option;
return _exhaustivenessCheck;

// 2. Call a function that accepts never
assertNever(option);

// 3-a. Use the satisfies syntax and abort
option satisfies never;
throw new Error("unreachable");

// 3-b. Use the satisfies syntax and return
option satisfies never;
return option;

Currently, 1 and 3 requires either throwing or returning to terminate the current (unreachable) execution branch. On the other hand, 2 automatically terminates it (assuming that the return type of assertNever is never).

My suggestion is that 3 (option satisfies never) also work as a termination point of the current execution branch, so that the following would be possible:

// 3-c (suggested behavior). Use the satisfies syntax and that's it!
option satisfies never;
/* code here is considered unreachable */

I think expr satisfies never; has several advantages compared to others:

Also, notably, expr satisfies never; is explicit enough for the compiler to reason about as part of the CFA.

In addition, I think this is also good from a theoretical perspective; never-returning functions are currently treated as a termination point of an execution branch because such functions can never return anything. Actually, this is not specific to functions. If you somehow have a value of type never, you have proven that this code isn't actually executed. expr satisfies never; feels like a good, sensible way to declare that you have a proof.

πŸ“ƒ Motivating Example

type Option<T> = {
  tag: "Some";
  value: T;
} | {
  tag: "None";
}


function optionToString(option: Option<unknown>): string {
  let value: string;
  switch (option.tag) {
    case "Some": {
      value = `Some(${option.value})`;
      break;
    }
    case "None": {
      value = "None";
      break;
    }
    default: {
      option satisfies never;
      // ↑ should have the same effect as:
      // assertNever(option);
    }
  }

  // Current behavior: error because `value` may not have been assigned.
  // Suggested behavior: no error here.
  return `Option(${value})`;
}

function assertNever(value: never): never {
  throw new Error("unreachable");
}

Playground

πŸ’» Use Cases

1. What do you want to use this for?

As shown above, for exhaustiveness checks.

2. What shortcomings exist with current approaches?

Live with less optimal (from different aspects) ways.

3. What workarounds are you using in the meantime?

assertNever(option);

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions