Open mfulton26 opened 3 years ago
I was hoping to write jsdoc for a case that I believe falls under this same problem since it relates to the use of same ToPropertyKey operation. The difference being I'd like to index an object using a class that implements toString()
. This is valid JavaScript
class Str {
toString() { return "str" }
}
class Prim {
[Symbol.toPrimitive]() { return "prim" }
}
const o = {}
o[new Str()] = 1
o[new Prim()] = 2
console.log(o)
// { str: 1, prim: 2 }
But trying to use such a type to index an object gives the "cannot be used as an index type".
Rather than carry additional information in the class, I have other methods to convert the type for proper presentation on JSON.stringify
, database, messaging interface etc. such that I'd not need to manually build datastructure with different types for each use.
Suggestion
π Search Terms
Symbol.toPrimitive
unique symbol
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.
I want to be able to use types with[Symbol.toPrimitive]
methods as properties in an interface.A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
I want to be able to use a value with a[Symbol.toPrimitive]
method as a property name.Type 'SomeType' cannot be used as an index type.
I want to be able to use a value with a[Symbol.toPrimitive]
method as an index type.π Motivating Example
π» Use Cases
I am working on a library that heavily uses
Symbol
s to add extension function to other types while avoiding name conflicts (asSymbol
s are unique). Instead of passing aroundSymbol
s directly I want to pass around richer objects with more information but, when passed in as an index type, I want the[Symbol.toPrimitive]
functions on my richer objects to be called and resolve to the appropriateSymbol
. Currently I am exploring either not supporting TypeScript initially (which is not ideal and could inhibit adoption) or ditch the idea of passing around richer objects and useSymbol
s directly (but this greatly reduces the usability and usefulness of the library).