microsoft / microsoft/TypeScript

Unary minus on 'any' should infer type to 'number | bigint', not just 'number'

Abierto
#60,914 3 comentarios 3 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

In Discussion Suggestion
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

https://www.typescriptlang.org/play/?target=99&ts=5.7.2#code/MYewdgzgLgBARjAvDAtACgIxgJQG4BQA9ITKTAHoD8+okscATEqnAcWRdTeNDGM+gx4iJMlW50+TZCjBtRpcRN4BDAFwwVYAJ7NWIjuNq8AHgJXzDXY7FoA3Zmjsat27EgB8qO5bHUbMABOzPZocMLsfsq24HbSME4aYACuALZwAKbBAD7wAJYA5nlgUO6IXig+BlEBgfH2DGERCpz4QA

💻 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

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. 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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.