microsoft / microsoft/TypeScript
Arithmetic operations on type-parameters subtyping `bigint` are incorrectly inferred to have type `number`
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
### 🔎 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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先运行链接的 Playground 复现,并确认推断出的类型和 TS2365 行为。然后跟踪 TypeScript 类型检查器针对受 bigint 约束的泛型类型参数进行算术运算的逻辑,为该示例添加或找到一个回归测试。当交集被视为 bigint,并且减去一个 number 会产生预期错误时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 38/100