# never() function

**URL:** <https://es.discourse.group/t/never-function/880>\
**Category:** 💡 Ideas\
**Created:** [July 20, 2021, 5:40pm UTC](https://es.discourse.group/t/never-function/880 "2021-07-20T17:40:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [July 20, 2021, 5:40pm UTC](https://es.discourse.group/t/never-function/880/1 "2021-07-20T17:40:12Z")

</div>

I propose we add a never function, that's defined somewhat like this:

```javascript
globalThis.AssertionError = class extends Error {}

globalThis.never = function(message = 'The program found itself in a bad state') {
  throw new AssertionError(message)
}

```

It's purpose it to help self-document when certain things should never happen, and to provide assertions to make sure they really don't happen.

Yes, it's easy for userland to define a function like this, but placing this within JavaScript makes it part of the common language that we can all read and understand instantly. `never()` looses a lot of it's self-documentation power if the code reader has to look up it's definition to figure out what it does.

**Motivating examples**

```javascript
// Throw an error if a particular type isn't handled
messageEvent.addListener(({ type, payload }) => {
  if (type === 'UNDO') {
    // ...
  } else if (type === 'REDO') {
    // ...
  } else never()
})

// Show that certain code paths should never execute
function getPlayer() {
  for (const entity of entities) {
    if (entity.type === 'PLAYER') return entity
  }
  never()
}

// Self-document that you expect something to always be found in the array
// (so you're not left wondering if `player` could be undefined)
const player = entities.find(e => e.type === 'PLAYER') ?? never()

// Assertions
function helperFunction() {
  if (!isInit) never()
  // ...
}

// Show that a function will never return
function runGameLoop() {
  while (true) {
    await new Promise(resolve => resolve(), 100)
    // ...
  }
  never()
}

```

**Prior art**

This is very loosely inspired by Elm's [never()](https://package.elm-lang.org/packages/elm/core/latest/Basics#never) function. Elm's never() was intended to help the type-system ensure that specific code paths never execute, while the never() I'm proposing here does a runtime-check instead.

**Related resources**

[Error.assert() thread](https://es.discourse.group/t/error-assert/356) - Love this idea - too bad it hasn't seen much attention yet.  
[throw as exception proposal](https://github.com/tc39/proposal-throw-expressions)

---

<div class="post-metadata">

**Author:** ![Lionad-Morotar](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lionad-morotar/32/548_2.png) [@Lionad-Morotar](https://es.discourse.group/u/Lionad-Morotar)\
**Post date:** [July 21, 2021, 1:57am UTC](https://es.discourse.group/t/never-function/880/2 "2021-07-21T01:57:58Z")

</div>

I think this pushes an unnecessary API concept and may introduce some anti-patterns...

```javascript
const neverBackup = never
never = (...args) => {
  console.trace()
  postError()
  neverBackup()
}

```

---

<div class="post-metadata">

**Author:** ![Lionad-Morotar](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lionad-morotar/32/548_2.png) [@Lionad-Morotar](https://es.discourse.group/u/Lionad-Morotar)\
**Post date:** [July 21, 2021, 2:02am UTC](https://es.discourse.group/t/never-function/880/3 "2021-07-21T02:02:02Z")

</div>

But it is true that you often see this kind of writing in the code  
但确实经常能在代码中看到这种写法

```javascript
const noop = () => { /* doNothing */ }

function elseFn () {
  while (true) {
    if (a) {
      doSomething()
    } else {
      noop()
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [July 21, 2021, 2:54am UTC](https://es.discourse.group/t/never-function/880/4 "2021-07-21T02:54:17Z")

</div>

I'm not sure I follow your example. What kind of unnecessary API concepts and anti-patterns are you talking about?

---

<div class="post-metadata">

**Author:** ![JackWorks](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jackworks/32/260_2.png) [@JackWorks](https://es.discourse.group/u/JackWorks)\
**Post date:** [August 4, 2021, 4:54am UTC](https://es.discourse.group/t/never-function/880/5 "2021-08-04T04:54:02Z")

</div>

We have a `unreachable(val: never, message: string): never` in our codebase. It is useful to make sure we made a exhausted match on a variable (the value will become type "never" when all possibility are excluded in Typescript)

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [August 12, 2021, 12:29pm UTC](https://es.discourse.group/t/never-function/880/6 "2021-08-12T12:29:28Z")

</div>

This conflicts very heavily with other "never" concepts, like `never()` from RxJS or a never-resolving promise. I'm also not sure why this isn't just called `assert`, if that's all it's doing.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [August 12, 2021, 1:44pm UTC](https://es.discourse.group/t/never-function/880/7 "2021-08-12T13:44:46Z")

</div>

I guess it could be `assertNever()` or something too. I'm not too attached to the name.

Alternatively, we could implement a normal assert function that takes a condition and a message, and if neither parameter is provided, it defaults to throwing an error.
