The pipeline operator has had a lot of discussion, but that is a good thing. We shouldn't rush things into the language. This idea does come up frequently, so it definitely feels like a problem worth solving. Hopefully someone on the committee agrees.
Nullish coalescing and optional chaining/evaluation are great, but one analog is still missing--
being able to short-circuit when the nullable value is in an argument position.
e.g. in a template string, passed to a function, used with arithmetic operators, etc.
Currently still need to use null checks and ternaries like
(val !== null && val !== undefined) ? transform(val) : undefined
or
(x != null && y != null) ? `${x} ${y}` : undefined
I'd like to suggest an operator to simplify this pa…
As there already exists a nullish coalescing operator (??), this implies a use case for an inverse nullish coalescing operator, which would still be a binary operator returning the second operand if the first one is not nullish (in contrast with ??).
Sample use-case: when fetching a possibly nullish relation:
let retrievedModel: Model | null = idOrNull === null ? null : await database.fetch<Model>(idOrNull); // the way it can be achieved now
let retrievedModel: Model | null = idOrNull !? await…
1 Like