Skip to content

Discriminated Union only auto resolve when it's basic props, not within callback #42023

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

Closed
alexis-regnaud opened this issue Dec 17, 2020 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@alexis-regnaud
Copy link

TypeScript Version: 4.1.2

Search Terms:
interdependent props, Union changed for an Intersection, Discriminated Union

Code

// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.

Expected behavior:
Since I use Discriminated Union I would like to have a Union within the callback as well.

Actual behavior:
Within the callback, I have an intersection instead of Union. But that work well with basic param (Exemple selected in the Playgound)

Playground Link:
Playground

Related Issues:
#40647, #12184

Seems like it's issue that has brought a bunch of times, but I'm wondering why we still need to do the

function isCase1(props: AllProps): props is Case1 {
  return !!props.aParamOfCase1;
}

Only for the callback function

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Dec 23, 2020
@RyanCavanaugh
Copy link
Member

This is a correct error; if this were allowed it'd be straightforward to inhabit an Option[] with an Option, which is unsound

@alexis-regnaud
Copy link
Author

It's a SimpleSelected | MultipleSelected so a Option | Option[] then newOption which is of type Option should be accepted. I'm kind of confused...

@RyanCavanaugh
Copy link
Member

// This call is legal, but crashes
aFunction({
  selected: [], onChange: (a: Option[]) => {
    console.log(a.indexOf(a[0]));
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants