microsoft / microsoft/TypeScript

{ [Symbol.toPrimitive]: () => Symbol }: support as index/property type

Open
#43,428 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Suggestion

🔍 Search Terms

  • Symbol.toPrimitive
  • unique symbol

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • 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.

⭐ Suggestion

  1. Instead of receiving 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.
  2. Instead of receiving 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.
  3. Instead of receiving 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

class UniquePerson {
  #symbol = Symbol();
  constructor(readonly name: string) {}
  [Symbol.toPrimitive]() {
    return this.#symbol;
  }
}
const sherlock = new UniquePerson("Sherlock Holmes");
interface PersonToAddress {
  [sherlock]: string;
  // ^ A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.
}
const personToAddress: PersonToAddress = {
  [sherlock]: "221B Baker Street",
  // ^ A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
};
personToAddress[sherlock]; //=> 221B Baker Street
               // ^ Type 'UniquePerson' cannot be used as an index type.

💻 Use Cases

I am working on a library that heavily uses Symbols to add extension function to other types while avoiding name conflicts (as Symbols are unique). Instead of passing around Symbols 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 appropriate Symbol. 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 use Symbols directly (but this greatly reduces the usability and usefulness of the library).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the three reported type-checking diagnostics for computed property names and index types in the motivating example. Done means values with [Symbol.toPrimitive] methods are accepted in interfaces, property names, and index expressions while preserving the requested type-checking behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.