Skip to content
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

Suggestion: error when using relational operators on types whose valueOf() method returns 'never' or 'void' #52773

Open
4 of 5 tasks
yseymour opened this issue Feb 15, 2023 · 4 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@yseymour
Copy link

Suggestion

πŸ” Search Terms

valueof never

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
    • (This would be a breaking change inasmuch as it will generate additional errors.)
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
    • (I hope so!)

⭐ Suggestion

At runtime, each operand of a relational operator is converted to a primitive value if needed by calling its valueOf() method (among other things). A type can be made effectively incomparable by giving it a valueOf method that throws an exception.

Ideally, tsc would check the valueOf signature of each operand, and produce a type error for any relational expression that implicitly invokes a valueOf whose declared return type is never or void.

πŸ“ƒ Motivating Example

The following code unconditionally throws at runtime and passes type-checking.

class C {
    valueOf() {
        throw new Error("Please don't call me again!")
    }
}
const a = new C();
const b = new C();
if (a < b) { // <- No error
    console.log("I am unreachable");
}

Playground link

πŸ’» Use Cases

TC39 Temporal PlainDate and Instant types throw an exception when compared this way (for reasonable reasons). It's easy to accidentally write code that naively compares these types using an operator instead of the appropriate compare method. It would be ideal if this was caught statically.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Feb 15, 2023
@shicks
Copy link
Contributor

shicks commented Mar 1, 2023

We're investigating defining some opaque types for bigint compatibility shims:

interface ShimBigint {
  [privateSymbol]: never;
  valueOf(): never;
}
declare global {
  function BigInt(arg: string|bigint|number|boolean|ShimBigint): bigint;
}
export function wrap(arg: string|bigint): ShimBigint {
  return (typeof BigInt === 'function' ? BigInt(arg) : arg) as unknown as ShimBigint;
}

But today you can still write wrap(123n) + 456 and you won't get any complaint. If we had this, then we could make that an error, which would be great!

@RyanCavanaugh
Copy link
Member

+ is almost certainly doomed to permanent weirdness due to Date, unfortunately.

@justingrant
Copy link
Contributor

On behalf of the Temporal champions, this proposal sounds great! πŸ‘

+ is almost certainly doomed to permanent weirdness due to Date, unfortunately.

The type of Date.prototype.valueOf doesn't have a type of never or void, so it wouldn't be affected by this proposal, right?

I agree that Date will be forever weird for + and many other reasons, but this proposal seems like it could be a way to use the type checker to warn users that they are doing something that may fail at runtime. What I particularly like is that it allows the type author to opt into this behavior rather than hard-coding something just for some types like Temporal.ZonedDateTime.

What do you think?

@justingrant
Copy link
Contributor

Also, would this proposal would apply to not just < and > but for all operators like + that also call valueOf?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants