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.

Divide by zero Typechecking

See original GitHub issue

We have type literals for numbers already and a strictNullCheck compiler option – wouldn’t it be awesome if we had a type guard again divide-by-zero errors? I’m building a game that involved a lot of math, computing intersections of lines and such. But what if a line is parallel? It would be cool if the compiler would suggest this as a type error.

function f(): number | 0 {
    return 0
}

const y = 100 / f()

TypeScript Version: 2.7.0-dev.201xxxxx

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
weswighamcommented, Feb 18, 2018

You can actually kinda already define an Infinity numeric literal type. Just make a type alias to a number that’s too big for JS to accurately represent and it’ll round to Infinity. The issue is that the global Infinity constant isn’t of this type 😉 , and declaration emit for this type may not work 😮 . It’s super hacky. We should really just add the NaN and Infinity numeric literal types (back). 🐱 Plus, NaN even has wonky comparability we should encode 😄

2reactions
RyanCavanaughcommented, Feb 15, 2018

Since this produces Infinity (which is meaningful) instead of NaN it’s hard to even interpret it as an error. intersectDistance(ray1, ray2) returns Infinity when they’re parallel… not a problem?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Division by zero in type theory: a FAQ | Hacker News
If I ever divide by zero (or by something that might be zero) in my proof, ... analogous to type-checking for template metaprogramming, ......
Read more >
Typescript type to prevent division by 0
Here you have very simple implementation: type NonZero<T extends number> = T extends 0 ? never : number extends T ? never :...
Read more >
How to handle divide by zero in a language that doesn't ...
What are my alternatives to handling a divide by zero error, ... It can be done much like ordinary typechecking, by evaluating all...
Read more >
Division by zero: a type and a non-trivial function to avoid ...
OP mentions dependent types, in which typechecking involves evaluation of expressions to the normal form (what you call "reduce"). Consequently, ...
Read more >
How does JavaScript handle Divide by Zero?
Divide by Zero is considered a special case by most programming languages. Any number can never be divided by zero because its result...
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