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.

Suggestion: stricter operators

See original GitHub issue

Currently operators like “+” are defined such that they match their semantics in JS. The below are all allowed by the compiler and produce the shown values, even with --strictNullChecks on.

  • 2 + 'a' => "2a"
  • null + 'a' => "nulla" (!)
  • 2 - null => 2

I propose letting users opt in (maybe via some --strictOperators) to strict operator behavior. Concretely I think this means:

  • restrict +, and += to just number and string, e.g. for the former only declare

    function +(a: number, b: number): number;
    function +(a: string, b: string): string;
    
  • restrict - and -= to just number

(any should continue to work as normal, of course.)

Relevant spec section: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#419-binary-operators

See also “Expression operators” in the strictNullTypes change: https://github.com/Microsoft/TypeScript/pull/7140 and in particular this rationale: https://github.com/Microsoft/TypeScript/pull/7140#issuecomment-186432250

This would fall under of “stricter” TypeScript, https://github.com/Microsoft/TypeScript/issues/274 .

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:15
  • Comments:14 (10 by maintainers)

github_iconTop GitHub Comments

7reactions
basaratcommented, Apr 10, 2016

number + string is actually quite common in JavaScript land and will probably not happen as it moves the convenience - type safety slider too much towards safety.

null + string should be disabled as a part of strictNullChecks.

Note:

A number of wat things are disabled in TypeScript e.g. [] + [] (valid JavaScript, produces “”) is an error in TypeScript. Similarly "hello" + 1 is allowed (like I mentioned) but "hello" - 1 is an error 🌹

2reactions
myitcvcommented, Apr 10, 2016

@basarat I really struggle when the argument “because that’s how Javascript does it” is applied. Particularly in cases like this where I think the cognitive load on the developer is increased by decisions to stick to the Javascript way. Not because it’s unclear how + behaves, rather that is creates cognitive dissonance with the rest of the type system:

let s: string;
let n: number;

s = n;            // ERROR: number is not assignable to string
n = s;            // ERROR: string is not assignable to number

let res = s + n;  // OK: really?

I understand that + is defined by the spec to work on combinations of number and string and so has well-defined behaviour, but given the example above I think it’s more confusing than useful for it to have been defined in this way (the Javascript way). Particularly when explicit conversion is so simple and more readable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Suggestion Backlog Slog, 6/8/2016 · Issue #9034 · microsoft ...
Suggestion : stricter operators #7989 Stricter binary operators. Clearly suspect for numeric/bitwise operands; make these an error; string + [whatever] is ...
Read more >
Equality comparisons and sameness - JavaScript | MDN
Strict equality compares two values for equality. ... Copy to Clipboard. Loose equality is only used by the == operator.
Read more >
Difference Between =, ==, and === in JavaScript [Examples]
=== (Triple equals) is a strict equality comparison operator in JavaScript, which returns false for the values which are not of a similar...
Read more >
JSON API reference | Programmable Search Engine
Boolean OR Search; Boolean Operators; Internationalizing Queries and Results ... We suggest you explicitly set the "hl" parameter in search ...
Read more >
Stricter national standards are required for credentialing of ...
One perspective suggested that biliary cannulation rate is a better ... 12 Should individual hospital needs for ERCP operators affect ...
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