Permit functions that return a value to also serve as a type guard #31376
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
Linear type, affine type, type guard
Suggestion
It would be very helpful to allow a function to serve as a type guard, but also return an unrelated value.
Use Cases
This can be used to express type changes as a result of mutating operations, covering some of the use cases of e.g. Rust's affine types. (See also #16148.)
Examples
Consider this example, compiled with
--strictNullChecks
:We could make this correct if
pop()
could both return a value and behave as a type guard. This isn't great syntax, but nonetheless consider if this was supported:Checklist
My suggestion meets these guidelines:
This can use a new, previously invalid syntax to avoid affecting any existing program.
There's no change in the code that's emitted; this feature would exist purely at the level of the type system.
I believe that it would. It seems to be aligned well, in particular, with "Statically identify constructs that are likely to be errors."
The text was updated successfully, but these errors were encountered: