Support !==
operator in is
and asserts
return type syntax
#55813
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
π Search Terms
unknown, assert, narrow, undefined, "is not"
β Viability Checklist
β Suggestion
This looks like something that would have been asked before, but it's hard to search for.
Right now
asserts
is a pretty magical syntax that lets you do:However, trying to express a more specialized function doesn't work
This is because
Exclude<unknown, undefined>
is still justunknown
, sinceunknown
is not just an alias for{} | null | undefined
. Assuming that won't change, how else can one expressassertNotUndefined
? Well, if we take the originalassert
function and try to retroactively rationalize its syntax as thiswhen called as
(hand-waving) just specializes to this
I don't care to bikeshed over whether the syntax should be
asserts proposition is not undefined
, whether the LHS or RHS operands can be flipped, whether!=
or other operators should exist, or if any type expressions other than primitive literals likeundefined
should be supported, so I'll leave that to the comments.π Motivating Example
π» Use Cases
A function that can exclude
undefined
can be composed with other functions, e.g.array.filter(isNotUndefined)
There's no clean way to define such a function without ugly conditionals to handle
unknown
.Rewrite code in more verbose style, e.g. manually construct a new array using a
for
loop instead ofarray.filter
.The text was updated successfully, but these errors were encountered: