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

Throw RangeError for invalid "mode" option? #38

Open
anba opened this issue Nov 14, 2024 · 7 comments
Open

Throw RangeError for invalid "mode" option? #38

anba opened this issue Nov 14, 2024 · 7 comments

Comments

@anba
Copy link

anba commented Nov 14, 2024

Iterator.zip and Iterator.zipKeyed, step 5:

  1. If mode is not one of "shortest", "longest", or "strict", throw a TypeError exception.

Temporal and ECMA-402 both throw a RangeError for invalid options:

Maybe consider changing step 5 to:

  1. If mode is not a String, throw a TypeError exception.
  2. If mode is not one of "shortest", "longest", or "strict", throw a RangeError exception.
@bakkot
Copy link
Collaborator

bakkot commented Nov 14, 2024

Hm. 262 uses RangeError almost exclusively for numeric arguments. We don't have many positions which take a limited set of strings, but for example Date.prototype[Symbol.toPrimitive] takes one of "string", "number", or "default", and throws a TypeError, not a RangeError, if given some other value.

I would prefer not to have two different error kinds here. I don't know whether it's worth breaking with 402, though. On the other hand, 402 and Temporal also do coercion of arguments, which we're no longer doing.

(Your observation applies equally to the stage-3 base64 proposal, incidentally.)

@ljharb
Copy link
Member

ljharb commented Nov 14, 2024

Personally, I definitely would find it more intuitive for an invalid enum value to throw a RangeError, since it's not the type that's wrong, it's that it's not in the set of allowed values.

@bakkot
Copy link
Collaborator

bakkot commented Nov 14, 2024

Uh. I don't know what "type" means if not "the set of allowed values".

@ljharb
Copy link
Member

ljharb commented Nov 14, 2024

lol i shouldn't have used the word "set" then - it's not in the finite list of allowed values maybe?

@bakkot
Copy link
Collaborator

bakkot commented Nov 14, 2024

A lot of types are finite lists? Boolean is just the two values true and false, e.g.

"These three specific strings" is no more or less of a type than "all strings". (For example TypeScript will let you represent that type just fine; I expect the types it will add for this proposal will reflect that.)

@anba
Copy link
Author

anba commented Nov 14, 2024

RangeError description also mentions "set or range of allowable values" → https://tc39.es/ecma262/#sec-native-error-types-used-in-this-standard-rangeerror.

I guess String.prototype.normalize is the counterexample where ECMA-262 uses a RangeError for an invalid string.

@bakkot
Copy link
Collaborator

bakkot commented Nov 14, 2024

Looking elsewhere, the web platform has a number of methods which take enums (parseFromString, Request/fetch's method), setTextRange, etc) and the modern handling for invalid values is always to throw a TypeError, though some legacy stuff does something different (e.g. insertAdjacentHTML throws a SyntaxError).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants