question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Union of generic variants

See original GitHub issue

It would be great if unionize could support union of generic variants

I’m using a generic interface in one of my projects to represent generic fetch status

export interface Fetch<T> {
  results: T[]
  finished: boolean
}

and I want it to become

export interface FetchLoading {
  type: 'loading'
}

export interface FetchSuccess<T> {
  type: 'success'
  results: T[]
  finished: boolean
}

export interface FetchSuccessWithErrors<T> {
  type: 'successWithErrors'
  results: T[]
  finished: boolean
  errors: string[]
}

export interface FetchError {
  type: 'error'
  reason: string
}

And I would create an union type which could be consumed by a UI component showing the fetch status according to its type.

How could we achieve that with unionize?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
twopcommented, Nov 19, 2018

Hey, I was thinking on that problem a lot and finally shipped a version that Im comfortable with: https://github.com/twop/ts-union

const Maybe = Union(val => ({
  Nothing: of<void>(),
  Just: of(val)
}));

const {Nothing, Just} = Maybe;

Just(1); // MaybeVal<number>
Just('str'); // MaybeVal<string>
Nothing<boolean>(); // MaybeVal<boolean>

Conceptually I have a special token type Generic and I replace it with an actual type like string at the moment of construction.

type CreatorFuncG<K, Rec> = K extends Of<infer A>
   ...
    ? A extends [Generic] 
    ? <P>(val: P) => UnionValG<P, Rec> : never
   ...
: never;

Might be applicable to unionize as well 😃

1reaction
pelotomcommented, Oct 18, 2019
  Just: ofType(a) //   'Just: a' might be more ergonomic

Yeah, I don’t see any reason to make it more verbose than it has to be.

{value: "val"}); // this is probably required because not every 'a' could be spread

Hmm, yeah, I wonder if we want to enforce that they pass a value property or just treat it as implied.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handbook - Unions and Intersection Types - TypeScript
An intersection type combines multiple types into one. This allows you to add together existing types to get a single type that has...
Read more >
typescript - Discriminated Union of Generic type - Stack Overflow
The problem. Type narrowing in discriminated unions is subject to several restrictions: No unwrapping of generics.
Read more >
Unions and interfaces - Apollo GraphQL Docs
Unions and interfaces are abstract GraphQL types that enable a schema field to return one of multiple object types. Union type.
Read more >
Custom Types · An Introduction to Elm
Note: Custom types used to be referred to as “union types” in Elm. Names from other communities include tagged ... The UserStatus type...
Read more >
TypeScript Fireworks: Union, Intersection and Variance
Give two types A and B , when put in a union A | B , a looser type is created. If we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found