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

Discriminating between two anonymous types #13459

Closed
johnfn opened this issue Jan 13, 2017 · 4 comments
Closed

Discriminating between two anonymous types #13459

johnfn opened this issue Jan 13, 2017 · 4 comments

Comments

@johnfn
Copy link

johnfn commented Jan 13, 2017

Say I have the following code:

function myFunction(x: { foo: number } | { bar: string }) {

How can I write some code to determine whether x is the first or second type?

Things I've considered:

  • Writing an x is MyType function to check for a foo property. Yes, I could do this, but it seems overkill for types that are only used as arguments to a single function.
  • if ((x as { foo: number}).foo) { let y = x as { foo: number }. I could do this, but it defeats the point of a type system. It's also not DRY.
  • Give them both a common type property. Again, seems like overkill for types that are only used as arguments for one function.
  • Unify both types into { foo: number, bar?: never } | { bar: string, foo?: never }. This feels like a hack that will confuse other developers when they look at my type signatures.

What I would like would be to do this:

if (x.foo) { /* x has been inferred to be { foo: number } */

Is there a reason that is not possible?

@svieira
Copy link

svieira commented Jan 13, 2017

In addition to if (x.foo) (which would be false if x had a foo with 0 as its value) it would be good if if ('foo' in x) and if (x.hasOwnProperty('foo')) could also do that sort of filtering of the input types.

@svieira
Copy link

svieira commented Jan 13, 2017

One other way of approaching the problem - add an isFoo check:

function isFoo(x: any): x is { foo: number } {
  return x && typeof x.foo === 'number';
}
function myFunction(x: { foo: number } | { bar: string }) {
  if (isFoo(x)) {
    x.foo;
  } else {
    x.bar
  }
}

@normalser
Copy link

#10485 tracks treating in operator as type guard

@johnfn
Copy link
Author

johnfn commented Jan 13, 2017

Ah yeah, #10485 is what I want!

@johnfn johnfn closed this as completed Jan 13, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants