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?
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.
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.
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.
JavaScript provides three different value-comparison operations: === — strict equality (triple equals) == — loose equality (double equals) Object.is()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With