Divide by zero Typechecking
See original GitHub issueWe 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:
- Created 6 years ago
- Comments:14 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 toInfinity
. The issue is that the globalInfinity
constant isn’t of this type 😉 , and declaration emit for this type may not work 😮 . It’s super hacky. We should really just add theNaN
andInfinity
numeric literal types (back). 🐱 Plus,NaN
even has wonky comparability we should encode 😄Since this produces
Infinity
(which is meaningful) instead ofNaN
it’s hard to even interpret it as an error.intersectDistance(ray1, ray2)
returnsInfinity
when they’re parallel… not a problem?