Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Relational operations should inferred for string as well #333

Open
MaxGraey opened this issue Apr 17, 2021 · 2 comments
Open

Relational operations should inferred for string as well #333

MaxGraey opened this issue Apr 17, 2021 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@MaxGraey
Copy link

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>

@JSMonk
Copy link
Owner

JSMonk commented Apr 18, 2021

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 JSMonk added the help wanted Extra attention is needed label Apr 18, 2021
@MaxGraey
Copy link
Author

MaxGraey commented Apr 18, 2021

"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 MaxGraey changed the title Relational operations should inerred for string as well Relational operations should inferred for string as well Apr 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants