{ [Symbol.toPrimitive]: () => Symbol }: support as index/property type
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 25/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Ferma
- Stack tecnologico
- javascript, typescript
- Ambito
- compilers
Direzione di ricerca
Inizia esaminando le tre diagnosi segnalate del controllo dei tipi per i nomi di proprietà calcolati e i tipi di indice nell’esempio motivante. Il lavoro è completato quando i valori con metodi [Symbol.toPrimitive] vengono accettati nelle interfacce, nei nomi di proprietà e nelle espressioni di indice, preservando il comportamento richiesto del controllo dei tipi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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).
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 15h
- PR unite (30g)
- 106
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di microsoft/TypeScript
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
microsoft/TypeScript#64322 · 2 commenti · 1 reazione · 2 assegnatari ·
-
Possible Improvement
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
microsoft/TypeScript#64278 · 1 commento · 1 reazione ·
-
Docs
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
microsoft/TypeScript#64118 · 1 commento ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
microsoft/TypeScript#64094 ·
-
Docs
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
microsoft/TypeScript#63959 · 5 commenti ·
Tutte le issue di microsoft/TypeScript
Issue simili
-
Type/Bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
OpenNSW/nsw-srilanka#497 ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 92/100
milvus-io/birdwatcher#545 ·
-
kind/bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
kubernetes-sigs/prow#953 · 1 commento ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
caddyserver/caddy#8046 ·