microsoft / microsoft/TypeScript
Generic recursive function infers incorrect return type
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
### 🔎 Search Terms
generic, recursive, return, infer
### 🕗 Version & Regression Information
- This changed between versions 5.4.5 and 5.5.4
- This bug continues to exist in nightly
### ⏯ Playground Link
https://www.typescriptlang.org/play/?ts=6.0.3#code/MYewdgzgLgBAZjAvDAPAFQDQD4AUBDALhkxgAciwBXAWwCMBTAJwEoksYBvAKBl5gEsEOAISlWjelEqMwMPAG4efCVJnwcpDGRgBaGAEZmXAL5cuoSLAAeSdQCI4IEHa0BWIxYggANvQB03iAA5jhWzEA
### 💻 Code
```ts
const f = (a: T, p: number) => {
if (!p) return a;
return f(p, p - 1) // f
}
const x = f("foo", 5) // inferred type "foo"
console.log(x) // 1
```
### 🙁 Actual behavior
The compiler infers `f`'s return type to be `T`. Then `x` is inferred to have type `"foo"` (or `string`), even though the actual return value is `1` (type `number`).
### 🙂 Expected behavior
In the example `f` is a recursive and generic function. Importantly, when recursing `f` calls itself on a _non-generic_ type.
If `p` is falsy then `f` returns `a`, which has the generic type `T`. This is the "base case". However if `p` is truthy then `f` calls itself with type parameter `number`, so in all the recursive cases the return type is actually `number`.
So the actual return type of `f` should be `T | number`.
### Additional information about the issue
In earlier versions of the compiler (I tested 5.4.5) recursive functions without explicit return type annotations are flagged as an error, so we sidestep this bug by requiring the return type to be manually declared:
> 'f' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions. (7023)
In later versions however the compiler seems to think it can infer the return type, leading to this incorrect inferred result. Interestingly even though the compiler infers `f` to return type `T`, if we add the explicit return type annotation `T` then the compiler actually becomes unhappy (geez, make up your mind):
```ts
const f = (a: T, p: number): T => {
if (!p) return a;
return f(p, p - 1) // Type 'number' is not assignable to type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to 'number'. (2322)
}
```
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der verlinkten TypeScript Playground-Reproduktion und vergleiche das Inferenzverhalten über die gemeldeten Compilerversionen hinweg. Verfolge, wie rekursive generische Rückgabetypen inferiert werden, wenn der rekursive Aufruf einen konkreten Typ verwendet, und füge anschließend einen Regressionstest für dieses Beispiel hinzu. Als erledigt gilt die Aufgabe, wenn die Funktion als Rückgabe von T | number inferiert wird und das falsch inferierte Ergebnis nicht mehr akzeptiert wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Aktiv
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 52/100