fullTemplateCheck not recognizing unary (+) operator and incorrect handing of (==) in template
See original GitHub issue🐞 bug report
Description
I’m finally getting around to using angularCompilerOptions
and have started to use fullTemplateCheck
. It seems as if while checking the templates, there is a failure to see the +
unary operator in front of the hours
variable (hours
is a string). The +
turns the string into a number so I believe this is an incorrect error, and therefore a bug?
Also it seems like fullTemplateCheck
is incorrectly handing ==
, as it incorrectly states that the result will always return false in the second code snippet below.
🔥 Exception or Error
Unary:
error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type. <div (click)="formatHours(+hours + 1)">
Doulble Equals:
error TS2367: This condition will always return 'false' since the types 'string' and 'number' have no overlap. <div [ngClass]="{'disabled': displayHours == 23}">
Environment
Angular Version:
Angular CLI: 9.0.4
Node: 10.16.1
OS: linux x64
Angular: 9.0.4
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.900.4
@angular-devkit/build-angular 0.900.4
@angular-devkit/build-optimizer 0.900.4
@angular-devkit/build-webpack 0.900.4
@angular-devkit/core 9.0.4
@angular-devkit/schematics 9.0.4
@angular/cdk 9.1.0
@angular/material 9.1.0
@ngtools/webpack 9.0.4
@schematics/angular 9.0.4
@schematics/update 0.900.4
rxjs 6.5.4
typescript 3.7.5
webpack 4.41.2
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Cast to negated unary expression fails in function templates
I kinda think it is a bug in clang. I fiddled around with this for a while. I tried: return equal( t1, t2...
Read more >Solving the “Unary Operator Expected” Error - Baeldung
In this tutorial, we'll look at the “unary operator expected” problem, often encountered while evaluating expressions in conditional ...
Read more >Template type checking - Angular
Use the $any() type-cast function in certain contexts to opt out of type-checking for a part of the expression; Disable strict checks entirely...
Read more >Unary plus (+) - JavaScript - MDN Web Docs
The unary plus (+) operator precedes its operand and evaluates to its operand but attempts to convert it into a number, if it...
Read more >How To Use JavaScript Unary Operators | DigitalOcean
Additionally, unary operators can not be overridden, ... and plus perform the same operation as the Number() function for non-numbers.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I can sympathize with TypeScript’s choice here. Very unexpected things can happen with these sorts of comparisons. For instance,
1000 == '1.e3'
istrue
in JavaScript 😉 You’d be better off by converting the number to a string instead, avoiding the problem.I edited my original comment for the unary operator with an alternative workaround to a method on the class. Your choice on which you choose.
I don’t think this issue will be fixed soon to be honest, but let’s hope it won’t be 3 years 😉 The occurrence of the issue from 2017 is less common.
My initial thought with the
==
was to use a+
in front of thedisplayHours
variable so that twonumber
s are being compared, however that brings me back to the main issue at hand here.