question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JoostKcommented, Mar 20, 2020

I can sympathize with TypeScript’s choice here. Very unexpected things can happen with these sorts of comparisons. For instance, 1000 == '1.e3' is true 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.

1reaction
serbboy23commented, Mar 20, 2020

My initial thought with the == was to use a + in front of the displayHours variable so that two numbers are being compared, however that brings me back to the main issue at hand here.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found