However, once we destructure, the "Either or ness" disappears. Type of thing and error are both Thing | undefined and Error | undefined. While basics like this work
if(error){// TS knows that `thing` is defined}
There are instances where you want to pass the whole tuple, maybeThing.
I'm curious what a good variable name is for this maybeThing.
Update: I recommend using an object style return. The array brackets are cute, but less useful. Positional args are fragile, and while this is only two, if you want to give a special error code or warning, then a simple new Error('asdf') can't represent either of these two (at least not very clearly).
So you may typically do:
return{error:newError('asdf')}
but you may occassionally:
return{warning:'Update skipped. Provided data is the same.',errorCode:'update_skipped',}
Note to self: I don't think I have any code that actually uses this idea. Was just a pure idea.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I think, this
safeCall
is nice when you are calling external libraries/functions that are hard to change to return[thing, error]
.I am currently converting a 4-5 throw statements to:
The nice thing about this, is that the consumer/caller does not need to check:
They can simply:
However, once we destructure, the "Either or ness" disappears. Type of
thing
anderror
are bothThing | undefined
andError | undefined
. While basics like this workThere are instances where you want to pass the whole tuple,
maybeThing
.I'm curious what a good variable name is for this
maybeThing
.Update: I recommend using an object style return. The array brackets are cute, but less useful. Positional args are fragile, and while this is only two, if you want to give a special error code or warning, then a simple
new Error('asdf')
can't represent either of these two (at least not very clearly).So you may typically do:
but you may occassionally:
Note to self: I don't think I have any code that actually uses this idea. Was just a pure idea.