8000 Easier union type narrowing in Angular control statements ('in' / `typeof` operator) · Issue #56875 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content

Easier union type narrowing in Angular control statements ('in' / typeof operator) #56875

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
johnameyer opened this issue Jul 7, 2024 · 2 comments

Comments

@johnameyer
Copy link
johnameyer commented Jul 7, 2024

Which @angular/* package(s) are relevant/related to the feature request?

compiler, core

Description

If I have a union of types i.e. { one: number } | { two: number } | { three: number }, it can be a bit onerous to narrow the type in my actual template description.

Attempting to write the template exactly as I might in a pure typescript file,

@if('one' in obj) {
    {{ obj.one }}
}

results in

Parser Error: Unexpected token 'in' at column 10 in ['one' in obj] in ...

The documentation does mention these operators are not supported, but chalks it up to side effects when these operators are pure.

You can't use JavaScript expressions that have or promote side effects, including:

  • Assignments (=, +=, -=, ...)
  • Operators such as new, typeof, or instanceof
  • Chaining expressions with ; or ,
  • The increment and decrement operators ++ and --
  • Some of the ES2015+ operators

Naturally, I could write out a type predicate function hasOne(obj: object): obj is { one: number} on my class, but this is a little verbose and can lead to a component having many such functions.

Proposed solution

If / else control expressions support in / typeof / instanceof operators like the other standard algebraic comparison operations.

Alternatives considered

Helper function like $any() that apply the proper type narrowing.

Do not throw errors for accessing property if one member of union contains that property.

@JoostK
Copy link
Member
JoostK commented Jul 7, 2024

I think this makes sense. There's a similar issue #38560 that has been rolled into #43485, I'm adding this one to that list as well.

@JoostK JoostK closed this as not planned Won't fix, can't repro, duplicate, stale Jul 7, 2024
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Aug 7, 2024
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

2 participants
0