microsoft / microsoft/TypeScript
Unary minus on 'any' should infer type to 'number | bigint', not just 'number'
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
🔎 Search Terms
"unary minus", "negate bigint"
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about bigint
⏯ Playground Link
💻 Code
const b = -(1n);
// ^?
const b2 = -b;
// ^?
const n = -(1);
// ^?
const n2 = -n;
// ^?
const a: any = b;
// ^?
const x = -a;
// ^?
const conv = (v: any) => -v;
// ^?
const r = conv(b);
// ^?
const conv2 = (v: number | bigint) => -v;
// ^?
const r2 = conv2(b);
// ^?
🙁 Actual behavior
Applying an unary minus on an any variable infers the type to number (as can be seen in the "hat-question mark" comments or the .D.TS tab for x, conv, and r). The inferred type for the constants x and r is thus wrong (they actually contain a BigInt value).
Unary minus on a number correctly infers number, and (more importantly) unary minus on a bigint correctly infers bigint. Same for unary minus on a number | bigint which infers number | bigint.
🙂 Expected behavior
Applying an unary minus on an any variable should infer the type to number | bigint.
ECMAScript specifies (https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-unary-minus-operator) unary minus as doing a ToNumeric (which returns either a Number of a BigInt) and then applying the appropriate unaryMinus abstract operation depending on the type of the value. The result is thus either a Number or a BigInt.
(the unary plus operator however converts to Number values only)
Additional information about the issue
No response
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 el TypeScript Playground enlazado y reproduce los tipos inferidos para el menos unario sobre any, number, bigint y number | bigint. Sigue el tratamiento que hace el compilador de la inferencia de tipos del menos unario y sus pruebas relacionadas; se considera terminado cuando el menos unario sobre any se infiere como number | bigint, mientras que el comportamiento existente para number y bigint sigue siendo correcto.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bien especificado
- Aptitud para principiantes
- 38/100