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

Disjoint unions without tags #3390

Closed
ghost opened this issue Feb 15, 2017 · 2 comments
Closed

Disjoint unions without tags #3390

ghost opened this issue Feb 15, 2017 · 2 comments

Comments

@ghost
Copy link

ghost commented Feb 15, 2017

I have this situation and no means to meaningful change the data structure. So I cannot add a tag.
Is there a way to distinguish the types without a tag? I tried ducktyping, but it does not work. See my example

    type Result = Done | Error; // a disjoint union type with two cases
    type Done = { count: number }
    type Error = { message: string }
    
    const doSomethingWithDone = (obj: Done) => {/*...*/}
    const doSomethingWithError = (obj: Error) => {/*...*/}
        
    const f = (result: Result) => {
      if (result.count) {
        doSomethingWithDone(result)
      } else {
        doSomethingWithError(result)
      }
    }
@AugustinLF
Copy link
Contributor

It does make sense, since your typings don't say that a Done cannot have a count property.

Using exact objects types seems to partially work, in the sense that it does refine properly, as you can see in this example. Sadly, you also have to do an explicit check in the else.

@nmote
Copy link
Contributor

nmote commented Feb 17, 2017

I answered this on Stack Overflow: http://stackoverflow.com/a/42281240/901387

@nmote nmote closed this as completed Feb 17, 2017
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