# Non-null assertion operator (aka Swift's force unwrap)

**URL:** <https://es.discourse.group/t/non-null-assertion-operator-aka-swifts-force-unwrap/1085>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [November 15, 2021, 11:18am UTC](https://es.discourse.group/t/non-null-assertion-operator-aka-swifts-force-unwrap/1085 "2021-11-15T11:18:09Z")\
**Posts on this page:** 1\
**Showing post:** 19

<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:** [November 18, 2021, 3:15pm UTC](https://es.discourse.group/t/non-null-assertion-operator-aka-swifts-force-unwrap/1085/19 "2021-11-18T15:15:41Z")

</div>

Another option, if we're not trying to constrain ourselves to the way TypeScript does its types, is to rely on pattern matching syntax for the assertion. It's an idea I brought up [over here](https://es.discourse.group/t/runtime-types-and-many-other-functional-ideas/1043) (but with a different purpose in mind). The syntax would basically be this (I tweaked the syntax here to make it similar to the syntax we've already been discussing)

```javascript
const user2 = user as { name } // works
const user2 = user as { xyz } // throw an error

```

The RHS is a pattern-matching expression from the [pattern matching proposal](https://github.com/tc39/proposal-pattern-matching). If the language also provides some default matchers, it would not be hard to allow null assertions as well.

```javascript
const user2 = null as Types.notNullish // throws an error
const user2 = user as Types.notNullish // works

```

Things like instanceof could easily be supported as well, as I explained over there (which I can elaborate on in this thread, if there's interest in going this direction).

Thus, there would be only one fairly simple piece of syntax to provide assertions for all three of these categories - non-nullish, typeof, and instanceof, along with other stuff like object shape.

---

_[View the full topic](https://es.discourse.group/t/non-null-assertion-operator-aka-swifts-force-unwrap/1085)._
