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

apply higher order type to each type within an union type #31899

Closed
5 tasks done
unional opened this issue Jun 14, 2019 · 4 comments
Closed
5 tasks done

apply higher order type to each type within an union type #31899

unional opened this issue Jun 14, 2019 · 4 comments

Comments

@unional
Copy link
Contributor

unional commented Jun 14, 2019

Search Terms

pick, union, transitive, type alias

Suggestion

Provide a way to define higher order type alias (such as Pick<>) which can apply the type transformation on each type with an union type, instead of combining them together into one type.

Use Cases

Using Pick<> or Omit<> on Discriminated Union types cause the result type no longer a discriminated union.

Examples

type Action = InvokeAction | ReturnAction

type InvokeAction = {
  type: 'invoke',
  id: string,
  payload: string[]
}

type ReturnAction = {
  type: 'return',
  id: string,
  payload: string
}

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>

let actions: Action[] =[]
function push(action: Omit<Action, 'id'>) {
  actions.push(action) // <-- error 'invoke' cannot assign to 'return'
}

The problem is Omit<Action, 'id'> creates this type:

{
  type: 'invoke' | 'return',
  payload: string | string[]
}

instead of

{  type: 'invoke', payload: string[] } | { type: 'return', payload: string }

Using Pick<> has the same problem, but use Omit<> in the example for a cleaner use-case.

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@jack-williams
Copy link
Collaborator

Duplicate of #28339

@unional
Copy link
Contributor Author

unional commented Jun 14, 2019

Thanks, it is.

@unional unional closed this as completed Jun 14, 2019
@unional
Copy link
Contributor Author

unional commented Jun 14, 2019

However, this issue demonstrate that it could break discriminated unions.

@RyanCavanaugh can you comment on this before I close it again? thx 🌷

@unional unional reopened this Jun 14, 2019
@unional
Copy link
Contributor Author

unional commented Jun 14, 2019

dup of #31501

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants