microsoft / microsoft/TypeScript
type parameter variance in generic call signature is incorrectly bivariant
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
### 🔎 Search Terms
unsound generic function assignability type parameter variance generic call signature bivariant
### 🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about **generics and variance**.
### ⏯ Playground Link
https://www.typescriptlang.org/play/?ts=6.0.3#code/C4TwDgpgBAygFgewK4BsAmAhCBJAdgNwEMAnAS0N2AB4ARAPigF4oqAVOgCgA8AuKVgGQ0ANFBB9WASkZ1BNAFDyAxglwBnYFDUQVuNH3jJ0WPETIVqSXAGtcCAO64GzKN1EhJTBiGWqNUZGAAOSQAWwAjCGIDRFRMHAISckoqXDDI4mctHVU0eUCQiKiqK1sHJw4AFlEAcg1iGskAOmAEAFEuMFUISnIUDklFXX9SXEKMmKN40ySLVPSorO5PGVcuFvbO7t7CfslRLkHhzVGAVRs7R0m4k0TzFNLLpyYoUfGo+TOL8o464AbavVGoogA
### 💻 Code
```ts
type ShouldBeInvariant = (x: T&D, y: T)=>T&D
const second: ShouldBeInvariant = (x, y) => y
const outNumber: ShouldBeInvariant = second
outNumber(4, 'str').toExponential()
const inNumber: ShouldBeInvariant = (x) => (x.toExponential(), x)
const inUnknown: ShouldBeInvariant = inNumber
inUnknown('str', 'str')
```
### 🙁 Actual behavior
The code type checks, while it shouldn't because that is unsound, using `ShouldBeInvariant` covariantly or contravariantly both causes runtime errors.
### 🙂 Expected behavior
`ShouldBeInvariant` should be invariant against its parameter. Both assigns:
```ts
const outNumber: ShouldBeInvariant = second
```
and
```ts
const inUnknown: ShouldBeInvariant = inNumber
```
should report errors
### Additional information about the issue
_No response_
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた TypeScript Playground の例から始め、ShouldBeInvariant を含む unsound な代入を再現してください。ジェネリックな呼び出しシグネチャの代入可能性と分散チェックを追跡し、両方の代入でエラーが報告され、無効な呼び出しが型チェックを通らなくなったことを示す回帰テストを追加してください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 48/100