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
On point 1: it may cause existing typescript code to fail to compile, but that would be because an error was found (in the case of the propagateTypeErasure option discussed below).
β Suggestion
Allow function return types to be intersections of concrete types and multiple type-assertions such that the function can both assert the types of its arguments AND produce a result (currently it is restricted to asserting a single argument's type and returning void OR it can return a value).
TS can already represent type erasure with a type assertion asserts arg is never, but when this is used it is very awkward to capture an output from the same function as the return value is required to be void.
Also add a compiler option propagateTypeErasure to require propagating type erasure to a calling function IF an argument of that function is directly passed into a called function which erases its type.
For the purposes of representing a protocol or transformation of values, it would be convenient to hide the transformation in a function such that a value which is passed in can no longer be used, and the value that comes out represents the new protocol state.
typeS1={a: number};typeS2={a: string};functiontransition(state: S1): S2&(assertsstate is never){/*...*/}declareconsts1: S1;consts2=transition(s1);// s1 is now 'never' type and cannot be reused
It would ALSO be convenient if multiple such assertions could be combined so that perhaps more values could have their types erased.
π» Use Cases
Providing APIs that properly represent the consumption of inputs while still being able to provide an output.
This suggestion has a narrow scope and does not purport to look for newer and deeper soundness or correctness guarantees, while still offering meaningful tools to API designers to more correctly express the behavior of their libraries in the type system.
The text was updated successfully, but these errors were encountered:
Suggestion
π Search Terms
type assertions
assertions
linear types
affine types
β Viability Checklist
My suggestion meets these guidelines:
On point 1: it may cause existing typescript code to fail to compile, but that would be because an error was found (in the case of the
propagateTypeErasure
option discussed below).β Suggestion
Allow function return types to be intersections of concrete types and multiple type-assertions such that the function can both assert the types of its arguments AND produce a result (currently it is restricted to asserting a single argument's type and returning void OR it can return a value).
Also add a compiler option
propagateTypeErasure
to require propagating type erasure to a calling function IF an argument of that function is directly passed into a called function which erases its type.I have explored this idea in this Playground
π Motivating Example
For the purposes of representing a protocol or transformation of values, it would be convenient to hide the transformation in a function such that a value which is passed in can no longer be used, and the value that comes out represents the new protocol state.
It would ALSO be convenient if multiple such assertions could be combined so that perhaps more values could have their types erased.
π» Use Cases
Providing APIs that properly represent the consumption of inputs while still being able to provide an output.
This suggestion has a narrow scope and does not purport to look for newer and deeper soundness or correctness guarantees, while still offering meaningful tools to API designers to more correctly express the behavior of their libraries in the type system.
The text was updated successfully, but these errors were encountered: