-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Divide by zero Typechecking #21955
Comments
Just to weigh in: given that
|
Since this produces |
As Daniel said, this only make sense for numbers known at compile time. Idris, famous for dependent types like this, uses Given an equivalent to Flow's To break that down, I'm catching the literal type of the denominator in a generic ( (I experimented with this idea in my PR for that feature (#17961), although my implementation ended up too glitchy, relying on synthetic type nodes that resulted in error tooltips showing up in the wrong place.) |
Wouldn't something like this work if Infinity was a type? declare function div(x: number, y: 0): Infinity
declare function div(x: number, y: number): number |
yes it would. :) |
Interesting. It appears nothing actually breaks until something needs and actual number. It would be cool if |
You can actually kinda already define an |
on second thought, I fear my partial function idea might well blow up as soon as it realizes you're potentially plugging in more than it could handle. hm. |
it seems @jcalz pulled off something like this at #21316 (comment) to disallow certain input |
I'd really love to see And adding a special case for the following doesn't seem too bad, declare const x : any;
if (x === NaN) {
//x is now of type `never`
} |
@AnyhowStep reminder that there are many NaNs in IEEE 754 floats, and JS adds its own layer of bizarreness on top: js> var a = 0/0;
NaN
js> var b = {} - 1;
NaN
js> a == b;
false
js> a === b;
false
js> isNaN(a);
true
js> isNaN(b);
true
js> NaN == NaN;
false It raises the potential question of whether to lump them together. |
Could we define Then the type of division between two |
By your definition, But their multiplication,
And their addition,
And subtraction,
|
Oh yeah, we've only got floats :( |
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.TypeScript Version: 2.7.0-dev.201xxxxx
The text was updated successfully, but these errors were encountered: