microsoft / microsoft/TypeScript

Arithmetic operations on type-parameters subtyping `bigint` are incorrectly inferred to have type `number`

Aperta
#61,429 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: check: Type Inference Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

### 🔎 Search Terms

bigint, operator, arithmetic, number, subtype, extend, generic, type parameter, incorrect, inference, inferred type, operation, 2365, TS2365

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about arithmetic/bigints/etc

### ⏯ Playground Link

https://www.typescriptlang.org/play/?target=99&ts=5.8.2#code/GYVwdgxgLglg9mABAIxlAzgUygSTFTAJy2gB4AhNdRTADwLABNrUBzGfAPgAoBDALkSUMAGhSDh6AJSCwIALbIiiAN4AoRJsQB6bYg4FimaPCQxqHYEUKZGiKHEQALXgDdM9gJ4AHDwAM5RSI-REJeKCdlCN4kPzYDPw0tCAR0KH18IhJYBEQAXkReRAAyFABuNSTNXUQAVgA6AA56gCZQzG84QgxEMEcoHw9rLpEqnT1kEHSiwnBYeSHCQi7EOAgIEGJ+MZqAbQBRACVDgF1BAGEYvvT5GFohGFY8aaZViKjB9DEQLBpabwANjAIGhECkwO5iKZqGMbFBNmZMkYTLkALSIACMFQAvpVweg4ADMPUAXBWNxUBhsM8ssYoNwAOxgMS1MBSKQVIA

### 💻 Code

```ts
function bitsetIntersect(a: Bits, b: Bits): number {
// intersection is inferred to have type `number` rather than `bigint`
const intersection = a & b;

// 5.8.2 reports no type error,
// but a runtime error occurs:
// [ERR]: Cannot mix BigInt and other types, use explicit conversions
return intersection - 1;
}

console.log(bitsetIntersect(7n, 5n));
```

### 🙁 Actual behavior

`intersection` is inferred to have type `number`. As a result, no type errors are reported.

When run, the code encounters a TypeError because `intersection - 1` attempts to perform arithmetic on a bigint and a number:

> [ERR]: Cannot mix BigInt and other types, use explicit conversions

### 🙂 Expected behavior

`intersection` should be inferred to have type `bigint`.

Thus the operation `intersection - 1` should report a type error:

> Operator '-' cannot be applied to types 'bigint' and 'number'. (2365)

### Additional information about the issue

This issue is most likely to be hit when _branding_ `bigint`, but it could also appear in other situations, such as using subtypes which are unions of literals.

---

I found some issues that are similar, but have a different scope. These issues were closed as intended, but in these issues, larger type bounds which include `bigint` _but also include `number`_ (such as `any` or `bigint | number`) are used:

* #39569
* #41741
* #42125
* #49558
* #60914

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo la riproduzione collegata di Playground e conferma il tipo inferito e il comportamento di TS2365. Poi segui la logica del type checker di TypeScript per le operazioni aritmetiche sui parametri di tipo generici vincolati da bigint, aggiungendo o individuando un regression test per l'esempio. Il lavoro è completo quando l'intersezione viene trattata come bigint e la sottrazione di un number produce l'errore previsto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.