Support updating types based on mutations made to an object when passed to a function, out keyword #26213
Closed
4 tasks done
Labels
Duplicate
An existing issue was already created
Search Terms
typeguard for mutating function arguments
Suggestion
As it stands there's really no way of updating types based on mutations made to an object when passed to a function. E.g.
However you can achieve something similar to the intended effect by using typeguards. E.g.
But as you can see this is a rather hacky approach and it isn't exactly self-documenting.
I suggest that either:
a)
The type system should be able to infer that the type of an object has been updated through an object reference. This is relevant since this is how js works either way, no matter if ts acknowledges it or not
b)
Add another ts-keyword that suggests that the function will modify the type of a parameter, maybe we could use the
out
keyword from C#?In the function scope an
out
argument should be considered aconst
value but not an immutable value.The
out
keyword is NOT optional.Use Cases
The first use case I've ran into, is when you are developing or using a library that decorates an object as part of the import. Using the
out
keyword would in these cases help making the code more self-documenting.Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: