microsoft / microsoft/TypeScript
{ [Symbol.toPrimitive]: () => Symbol }: support as index/property type
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
Suggestion
🔍 Search Terms
Symbol.toPrimitiveunique 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
- 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. - 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. - 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).
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der Überprüfung der drei gemeldeten Diagnosen der Typprüfung für berechnete Eigenschaftsnamen und Indextypen im zugrunde liegenden Beispiel. Als abgeschlossen gilt die Arbeit, wenn Werte mit [Symbol.toPrimitive]-Methoden in Interfaces, Eigenschaftsnamen und Indexausdrücken akzeptiert werden und dabei das angeforderte Verhalten der Typprüfung erhalten bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 25/100