You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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,
results in
The documentation does mention these operators are not supported, but chalks it up to side effects when these operators are pure.
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.
The text was updated successfully, but these errors were encountered: