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

Type narrowing for `NgSwitch` and `NgSwitchCase` #20780

Open
chuckjaz opened this issue Dec 4, 2017 · 6 comments
Open

Type narrowing for `NgSwitch` and `NgSwitchCase` #20780

chuckjaz opened this issue Dec 4, 2017 · 6 comments

Comments

@chuckjaz
Copy link
Contributor

@chuckjaz chuckjaz commented Dec 4, 2017

Current behavior

No type narrowing is performed when using NgSwitch and NgSwitchCase.

Expected behavior

Narrow the types used in the NgSwitchCase based on the selection criteria.

See #17953 (comment) and following for more details.

@dirkluijk
Copy link
Contributor

@dirkluijk dirkluijk commented Feb 1, 2018

Looking forward to see type guard support in language-service as well!

My simple use case:

interface Animal {
  type: 'fish' | 'bird';
}

interface Fish extends Animal {
  type: 'fish';
  foo: string;
}

interface Bird extends Animal {
  type: 'bird';
  bar: string;
}

@Component({})
class MyComponent {
  animal: Animal;

  isFish(animal: Animal): animal is Fish {
    return animal.type === 'fish';
  }

  isBird(animal: Animal): animal is Bird {
    return animal.type === 'bird';
  }
}

Use case 1

<app-fish *ngIf="isFish(animal)" [foo]="animal.foo"></app-fish>
<app-bird *ngIf="isBird(animal)" [bar]="animal.bar"></app-bird>

Use case 2

<ng-container [ngSwitch]="true">
  <app-fish *ngSwitchCase="isFish(animal)"></app-fish>
  <app-bird *ngSwitchCase="isBird(animal)"></app-bird>
</ng-container>

Are both use cases covered?

@waterplea
Copy link
Contributor

@waterplea waterplea commented Sep 9, 2019

This would be a great addition, having an issue similar to case 2 that @dirkluijk posted.

@amitport
Copy link
Contributor

@amitport amitport commented Oct 31, 2019

@chuckjaz any updates? Anyone working on this? Thanks

@dirkluijk
Copy link
Contributor

@dirkluijk dirkluijk commented Jan 21, 2020

Will this be solved in Ivy?

@SchnWalter
Copy link
Contributor

@SchnWalter SchnWalter commented Jan 21, 2020

No, this isn't solved by Ivy, if anything, Ivy will cause more people to realize how badly they need ngSwitch to support type narrowing.

@klemenoslaj
Copy link
Contributor

@klemenoslaj klemenoslaj commented Jun 24, 2020

This would really clean up a lot of template code. How come this is such a low priority?

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

Successfully merging a pull request may close this issue.

None yet
8 participants
You can’t perform that action at this time.