type-guards
type-guards copied to clipboard
Filtering out specific symbols
Symbols are great for special values where "null" and/or "undefined" already represent a valid value. Since they are unique, there is no fear of "what if the consumer wants exactly that value?"-type of questions. Of course, there are other possibilities such as wrapping the unknown-yet-highly-customizable value in an object such as { isReady: false } | { isReady: true, value: T}
; however, another equally valid approach is T | typeof NOT_READY
, where const NOT_READY = Symbol('NOT_READY')
.
When we want to filter out these "special" values, for one reason or another (ignoring them, skipping them, waiting only for them), we might encounter code like this:
filter(event => event != NOT_READY)
Obviously, this is not strict enough and would require a specific guard. This is precisely what this module was created for, and while tg.isNot(NOT_READY)
seems like it should work, it doesn't. Not sure if bug, or ignorance of Symbols. Either way, would like to have this.