microsoft / microsoft/TypeScript
Improve support for Symbol.toPrimitive
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
Presently, TS limits index types to 'string', 'number', 'symbol', or 'any'. That's fine, but consider the following:
class Foo {
[Symbol.toPrimitive](): number {
return 1;
}
}
var x = ['a','b','c'];
var f = new Foo();
x[f];
We give the error an index expression argument must be of type ... on the array access.
Foo is for all intents and purposes a wrapper around the number 1 (close to new Number(1)), yet it cannot be used to index an array, since Foo itself is not of type number, string, symbol, or any. We could do extra type-checking to see if an object has a user-defined Symbol.toPrimitive and consider its return types when checking type-coercing operations (after all, if you've opted in to using Symbol.toPrimitve it's probably part of your goal to gain this behavior), such as indexing or mathematics operators.
Building on this, we currently don't support the coercion of Number or String classes to their respective primitive types (which are a specialization of the above issue but which applies to ES5 and ES3 output). Presently one can't do this:
function add(a: string, b: string): number {
var x = new Number(a);
var y = new Number(b);
return x + y;
}
add('10e4', '0x8');
Because since neither x nor y is the 'number' (or 'string') primitive type, the addition operation is not allowed. Though it's still valid js (and without the usage of the Number class, the addition would result in '10e40x8'!).
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.
Línea de trabajo
Comienza con los ejemplos de expresiones de índice y de suma del issue; después, sigue las reglas de comprobación de tipos para los argumentos de índice y los operandos aritméticos. Se considera terminado cuando se tienen en cuenta de forma coherente Symbol.toPrimitive y la coerción de los wrappers Number/String sin permitir operaciones no válidas.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100