microsoft / microsoft/TypeScript
Disallow values of type 'symbol' from being passed to 'Number' function/constructor or 'String' constructor
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
Suggestion
Change the NumberConstructor and StringConstructor interfaces to disallow passing values of type Symbol. Currently it accepts values of type any.
Attempting to call Number(Symbol()), new Number(Symbol()), or new String(Symbol()) all throw TypeErrors at runtime.
Note: String(Symbol()) is fine - only new String(Symbol()) will throw.
Use Cases
Any time a value of type any is passed to Number, there is a risk of that value being a symbol and throwing a TypeError.
Object keys are explicitly allowed to be of type symbol (as number | string | symbol). Passing an arbitrary object key to Number can throw a TypeError as well.
Examples
Checking if a value can be coerced to a number
I just ran into this issue when checking if an object key could be a valid array index. The following should be an error as it will throw if value is of type symbol.
function keyCanBeArrayIndex(value: string | number | symbol): boolean {
return !Number.isNaN(Number(value));
}
Having this be invalid would have forced me to write something like:
function canBeArrayIndex(value: string | number | symbol): boolean {
try {
return !Number.isNaN(Number(value));
} catch {
return false;
}
}
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, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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 localizando las declaraciones de las interfaces NumberConstructor y StringConstructor en las definiciones de las bibliotecas de TypeScript y revisa las pruebas existentes para la comprobación de argumentos de constructores. Confirma la distinción deseada entre Number(Symbol()), new Number(Symbol()), String(Symbol()) y new String(Symbol()), y después añade cobertura que muestre los diagnósticos de compilación previstos. Se considera terminado cuando los valores de tipo symbol se rechacen solo donde lo especifica el issue, sin cambiar el JavaScript emitido.
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
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 45/100