Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSLint double vs triple equality

Tags:

I know that a single equality sign means assignment; double means equality; and triple means equality and the same type.

What I don't understand why the typescript linter would want me to use triple equality signs in this case:

function gcf(a: number, b: number): number {     return (b == 0) ? (a) : (gcf(b, a % b)); } 

TsLint: == should be ===

I know that 0 is a number and I also know that b is a number (or else I'll get a compilation error). So why would I want to use triple equality signs in this case?

like image 457
user886079 Avatar asked Mar 29 '14 15:03

user886079


People also ask

What is the difference between double equal and triple equal?

Double Equals ( == ) checks for value equality only. It inherently does type coercion. This means that before checking the values, it converts the types of the variables to match each other. On the other hand, Triple Equals ( === ) does not perform type coercion.

What is == and === in TypeScript?

The Typescript has two operators for checking equality. One is == (equality operator or loose equality operator) and the other one is === (strict equality operator). Both of these operators check the value of operands for equality.

What is TSLint rules?

TSLint is an extensible static analysis tool that checks Javascript and TypeScript code for readability, maintainability, and functionality errors. It can be integrated into build systems and editors. It has a set of core rules built into and configuration that allows it to be extended with custom rules.

What is triple === in JavaScript?

JavaScript provides three different value-comparison operations: === — strict equality (triple equals) == — loose equality (double equals) Object.is()


1 Answers

Types can't save you from all errors caused by ==. Particularly since undefined and null are compatible with all types. e.g. the following is an incorrect if :

var foo:number = null;   if (foo == undefined) {      console.log('is undefined'); // actually null   } 

For more info on why these are equal https://stackoverflow.com/a/359509/95190

Personally : I have had this rule disabled and never had any issues. I don't compare with true/false/null/undefined, just if them. And typescript prevents comparing strings and numbers so that is not an error I need to deal with.

like image 132
basarat Avatar answered Oct 06 '22 19:10

basarat