Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow intersection of return types and multiple type-assertions #50006

Closed
5 tasks done
Geordi7 opened this issue Jul 22, 2022 · 2 comments
Closed
5 tasks done

Allow intersection of return types and multiple type-assertions #50006

Geordi7 opened this issue Jul 22, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Geordi7
Copy link

Geordi7 commented Jul 22, 2022

Suggestion

πŸ” Search Terms

type assertions
assertions
linear types
affine types

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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.

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.

type S1 = {a: number};
type S2 = {a: string};

function transition(state: S1): S2 & (asserts state is never) {/*...*/}

declare const s1: S1;
const s2 = 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.

@whzx5byb
Copy link

whzx5byb commented Jul 23, 2022

Duplicate of #40562 and #42253

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 28, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants