-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Improve support for Symbol.toPrimitive #4538
Comments
This is (imo) related to the |
Aye, handling |
@weswigham I've started working on a solution to this over in #2361 |
I thought I might try the hack where you intersect with a primitive. This works for alot of cases.
However I found that strings intersection results in conflicts with the main type. Especially in terms of index accessors:
So it would be nice to support some way of doing this. Although I noticed that |
The code works, but TypeScript does not allow it. |
Presently, TS limits index types to 'string', 'number', 'symbol', or 'any'. That's fine, but consider the following:
We give the error
an index expression argument must be of type ...
on the array access.Foo
is for all intents and purposes a wrapper around the number1
(close tonew Number(1)
), yet it cannot be used to index an array, sinceFoo
itself is not of typenumber
,string
,symbol
, orany
. We could do extra type-checking to see if an object has a user-definedSymbol.toPrimitive
and consider its return types when checking type-coercing operations (after all, if you've opted in to usingSymbol.toPrimitve
it's probably part of your goal to gain this behavior), such as indexing or mathematics operators.Building on this, we currently don't support the coercion of
Number
orString
classes to their respective primitive types (which are a specialization of the above issue but which applies to ES5 and ES3 output). Presently one can't do this:Because since neither x nor y is the 'number' (or 'string') primitive type, the addition operation is not allowed. Though it's still valid js (and without the usage of the Number class, the addition would result in '10e40x8'!).
The text was updated successfully, but these errors were encountered: