{ [Symbol.toPrimitive]: () => Symbol }: support as index/property type
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 25/100
- Tipo de issue
- Nueva funcionalidad
- Claridad
- Bien especificado
- Estado de actividad
- Estancado
- Stack tecnológico
- javascript, typescript
- Área
- compilers
Línea de trabajo
Comienza revisando los tres diagnósticos reportados de comprobación de tipos para nombres de propiedades calculados y tipos de índice en el ejemplo motivador. Se considera terminado cuando se aceptan valores con métodos [Symbol.toPrimitive] en interfaces, nombres de propiedades y expresiones de índice, preservando el comportamiento solicitado de comprobación de tipos.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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).
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de microsoft/TypeScript
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
microsoft/TypeScript#64322 · 2 comentarios · 1 reacción · 2 asignados ·
-
Possible Improvement
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
microsoft/TypeScript#64278 · 1 comentario · 1 reacción ·
-
Docs
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
microsoft/TypeScript#64118 · 1 comentario ·
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 88/100
microsoft/TypeScript#64094 ·
-
Docs
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
microsoft/TypeScript#63959 · 5 comentarios ·
Todos los issues de microsoft/TypeScript
Issues similares
-
optimization optimization:agents-md-curator
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
githubnext/gh-aw-cao#13143 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
blinklabs-io/bursa#904 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
yanet-platform/ipfw-go#129 ·
-
bug confmap/provider/googlesecretmanagerprovider needs triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
open-telemetry/opentelemetry-collector-contrib#51273 · 2 comentarios ·
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 90/100