hegel icon indicating copy to clipboard operation
hegel copied to clipboard

Relational operations should inferred for string as well

Open MaxGraey opened this issue 3 years ago • 2 comments

function le(a, b) {
    return a <= b
}

Actual: a and b infer as $StrictUnion<bigint | number>

Expected a and b infer as $StrictUnion<bigint | number | string>

MaxGraey avatar Apr 17 '21 08:04 MaxGraey

Actually, Should it have a behavior like this? In this case, strings will be cast implicitly into char codes for the comparison. One of the Hegel.js design goals is to avoid implicit type casts for predictability and type safety. But, I'm not sure, that we should avoid those kinds of typecasting. So I want to discuss it here.

JSMonk avatar Apr 18 '21 17:04 JSMonk

"a" <= "b", "a" == "a", "a" != "b" and etc is fully valid binary operations for strings and doesn't required any implicit casts.

Real world example:

const arr = [{ key: "b" }, { key: "a" }];
arr.sort((a, b) => Number(a.key > b.key) - Number(a.key < b.key)) // Number(op) need for explicit cast from Boolean to Number

MaxGraey avatar Apr 18 '21 18:04 MaxGraey