microsoft / microsoft/TypeScript
type parameter variance in generic call signature is incorrectly bivariant
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
### 🔎 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_
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia dall'esempio collegato di TypeScript Playground e riproduci le assegnazioni non sicure che coinvolgono ShouldBeInvariant. Traccia l'assegnabilità delle firme di chiamata generiche e il controllo della varianza, quindi aggiungi un test di regressione che mostri che entrambe le assegnazioni segnalano errori e che le chiamate non valide non superano più il controllo dei tipi.
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à
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 48/100