Logo

dev-resources.site

for different kinds of informations.

the (not so big) Bang!

Published at
11/1/2023
Categories
typiscript
null
Author
hesxenon
Categories
2 categories in total
typiscript
open
null
open
Author
8 person written this
hesxenon
open
the (not so big) Bang!

Premise

So I've just read this article here in which the author basically throws typescripts warning regarding potentially undefined values into the wind with the bang operator.

In short this allows you to do something like this:

const maybeElement = document.getElementById("foo");
maybeElement!.addEventListener(...); // <-- notice the ! before the .
Enter fullscreen mode Exit fullscreen mode

So with this we can tell typescript to ignore the problems of accessing a property on a potentially null value.

My initial thoughts on this were "yeah, and if you'd use that in my codebase without a comment we'd have a stern talk" and while I stand by that - at least put a comment there on why you're sure it's not nullish - there is something to be said in favor of telling typescript to shut up; especially in a case like hers.

The Problem

So lets assume we don't want to do this and we just use the elvis operator (?.) instead -> maybeElement?.addEventListener. Great, now our code doesn't crash when the element isn't defined.

But think again... in such a scenario, what would be easier to trace? If the id of the element changes, maybeElement becomes null and with the bang operator it crashes "loudly" a few lines below.

If we just mindlessly change the ! into a ? it doesn't crash - but the event listener also isn't added which in turn means that we probably have a far more intricate and difficult to track bug on our hands. Maybe that bug even isn't discovered for a day or two and then what? Are you confident enough that you'll find the mistake within the same amount of time and effort as when the ! was still there?

The solution

Be mindful about your operators. It's not like ?. has no drawbacks and failing early is typically better. If you want to express "I need this to be non nullish" using ! could be acceptable - of course the best solution here would be to take a step back and think about what to do in case the element isn't found, which is why typescript warns you in the first place.

Conclusion

  • no such thing as mindless programming
  • ?. can easily be abused to delay surfacing of errors
  • using ! probably means you should think harder about proper errors. Maybe an either/result monad as encapsulated by various libraries like fp-ts and effect?
null Article's
30 articles in total
Favicon
How Imburse Payments Ships High-Quality APIs Faster
Favicon
Need to Verify Your JSON Schema? Here's a Few Ways to Do It!
Favicon
Code Smell 260 - Crowdstrike NULL
Favicon
Be careful when using NULL in PostgreSQL
Favicon
The most painful reason NULLs are evil
Favicon
Null or Nothing? Unmasking the Mystery of Parameters in Dart
Favicon
La Solución del Billón de Dólares
Favicon
11 Lessons to learn when using NULLs in PostgreSQL®
Favicon
NULLs Are Not The Same – A Guide
Favicon
the (not so big) Bang!
Favicon
Rust's Option type... in Python
Favicon
Understanding Nullable Reference Types in C#
Favicon
Working with NULL in Databases. Turn Your Frustration Into Delight
Favicon
TypeScript: The many types of nothing
Favicon
ERROR: null" or "null pointer exception while invoking FlowService - Storage get operation
Favicon
ServiceNow: 1 thing for safer GlideRecord scripts
Favicon
How NullPointerException can be avoided in Java
Favicon
Consider these facts when dealing with NULL in RDBMS
Favicon
Unhandled Exception: type 'Null' is not a subtype of type 'int' in type cast error when trying to call function with no int
Favicon
When <nil> is not <nil>
Favicon
Absence of null in Solidity
Favicon
How to Check for Null in Javascript
Favicon
Javascript Tagalog - Null
Favicon
Kotlin 基礎 Part 1 -- !! や ?: と ?.let で Nullable な値を処理する
Favicon
Remove null check, use the Optional
Favicon
Handling null: optional and nullable types
Favicon
More JS Concepts
Favicon
Valores null e undefined no JavaScript
Favicon
Javascript default parameter for null and undefined
Favicon
How I Learned to Stop Worrying and Love NULL in SQL

Featured ones: