Proposal: Add a strictly typed spread operator #30826
Labels
Suggestion
An idea for TypeScript
Too Complex
An issue which adding support for may be too complex for the value it adds
Search Terms
There are a few related issues closed with a link to #12632 (For example #17422) but it's a lot more generic, this proposal is very specific to the spread operator where I believe there is a relatively simple fix that would handle a lot of situations.
Suggestion
Add an operator similar to the spread operator that ensure that the type of the resulting object is the same as the type of the input one.
It would serve the exact same purpose as the
with
operator in F#, the|
operator in Elm or the...
operator in ReasonML.For example:
The following limitations would apply:
It would be compiled to
...
as the difference is only relative to typecheckingUse Cases
The central use case is to mutate objects that are used as immutable like React state or Redux state.
While it currently work most of the time as in the end the signature generated by the spread operator will be matched against the real type it produce problems:
Examples
Would also work for anonymous types :
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: