does arktype understand "minimum 0 but not -0"?

in zod, even if you put .nonnegative it will still accept negative zero. would the same happen with AT?
No description
7 Replies
LukeAbby
LukeAbby4w ago
I tried this in the Arktype playground:
import { type } from "arktype";

const Thing = type("number >= 0")
const out = Thing(-0)
import { type } from "arktype";

const Thing = type("number >= 0")
const out = Thing(-0)
and it passed so presumably yes https://arktype.io/playground?code=import%2520%257B%2520type%2520%257D%2520from%2520%2522arktype%2522%250A%250Aconst%2520Thing%2520%253D%2520type%28%257B%250A%2509name%253A%2520%2522string%2522%252C%250A%2509%2522versions%253F%2522%253A%2520%2522%28number%2520%257C%2520string%29%255B%255D%2522%250A%257D%29%250A%250Aconst%2520out%2520%253D%2520Thing%28%257B%250A%2520%2520%2520%2520name%253A%2520%2522TypeScript%2522%252C%250A%2520%2520%2520%2520versions%253A%2520%255B%25225.8.2%2522%252C%25206%252C%25207n%255D%250A%257D%29%250A fwiw it's pretty easy to fix;
type("number").narrow((data, ctx) => {
// Uses `Object.is` because regular equality treats -0 and 0 as identical.
if (data > 0 || Object.is(data, 0)) {
return true;
}
return ctx.reject("a positive number");
});
type("number").narrow((data, ctx) => {
// Uses `Object.is` because regular equality treats -0 and 0 as identical.
if (data > 0 || Object.is(data, 0)) {
return true;
}
return ctx.reject("a positive number");
});
Though @ssalbdivad might want to know that the error message for -0 was must be a positive number (was 0) which is mildly misleading because it was -0 not that I blame you (-0).toString() is "0"
Dimitri Mitropoulos
it's a bit on-the-line what to do here
LukeAbby
LukeAbby4w ago
In fairness the exact code I did was number >= 0 and -0 >= 0 is true
Dimitri Mitropoulos
the IEEE-754 spec says that 0.0 and -0.0 are considered equal (last I checked) right
LukeAbby
LukeAbby4w ago
yeah they are it's why I had to use Object.is in the narrow to make -0 an error but not 0 otherwise I'd just have done data > 0 || data === 0 but in reality that'd allow -0 first time I've seen a legitimate use case tbh
Dimitri Mitropoulos
yeah, cool, thanks
ahrjarrett
ahrjarrett4w ago
i've run into issues with zod not differentiating btwn 0 and -0 before, i think it had to do with serialization/deserialization would be nice to invert control somehow and let devs decide what it means for 2 numbers to be equivalent

Did you find this page helpful?