You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So I need to check the type again or cast it, e.g. using path!. But this is redundant! Since we have called getExtension() before and the execution has continued here, we already know that the path exists and is not undefined. I would like to able to write a declaration l like this:
Duplicate of #40562 for the assertion type predicate part.
I feel like I've seen discussion of non-boolean non-assertion type predicates before, but I can't find it now. Maybe #12798 (combine type predicate types) and #15048 (one-sided type predicates) and #29317 (negated types)? So (0 & x is Foo) | (1 & x is not Foo) or something?
Edit: #46650 is the currently-open issue about non-boolean type predicates.
Suggestion
π Search Terms
TS1228, type predicate, type guards
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Type predicates using the
asserts
keyword should be combinable, i.e. intersectable, unionable, etc. with other types.π Motivating Example
Consider this snippet:
Currently, the compiler complains in the last line of
stripExtension()
:So I need to check the type again or cast it, e.g. using
path!
. But this is redundant! Since we have calledgetExtension()
before and the execution has continued here, we already know that the path exists and is notundefined
. I would like to able to write a declaration l like this:Formally, I propose that function return types should support the combination of types that includes
asserts
and other type predicates (if any).Furthermore, tsc should also be able to infer this information from the implementation of the function.
π» Use Cases
path!.slice(...)
), or in more complicated scenarios,(<string>path).slice(...)
)The text was updated successfully, but these errors were encountered: