microsoft / microsoft/TypeScript
Wrong inference for defaulted generic in nested call
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
nested generic call inline inference contextual typing function calls default type parameter
🕗 Version & Regression Information
- I see the same issue from version 4.1.5 to nightly in TypeScript Playground
⏯ Playground Link
💻 Code
declare function join<T extends string, U extends string, V extends string = ''>(start: T, end: U, sep?: V): `${T}${V}${U}`
declare function expect<T>(value: T): { toBe(expected: T): void }
expect(join('Hello', 'World')).toBe('HelloWorld') // bad
const withoutSeparator = join('Hello', 'World')
// ^? const withoutSeparator: "HelloWorld"
expect(withoutSeparator).toBe("HelloWorld") // good
expect(join('Hello', 'World', ' ')).toBe('Hello World') // good
🙁 Actual behavior
TypeScript does not infer the return types correctly of a nested function call when an optional generic param is omitted, even if a default is provided. When the function call is not nested, it is inferred correctly. It is also inferred correctly when all params are provided.
🙂 Expected behavior
I would expect that both of these approaches would compile:
expect(join('Hello', 'World')).toBe('HelloWorld') // bad
const withoutSeparator = join('Hello', 'World')
expect(withoutSeparator).toBe("HelloWorld") // good
Additional information about the issue
There are very similar issues, such as #54184, #56714, and #62680 but they didn't quite match up to what I'm seeing here, particularly since the reproductions are quite a bit more complex.
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 damit, die verknüpfte TypeScript Playground-Reproduktion auszuführen, und vergleiche den verschachtelten Aufruf mit dem eigenständigen und dem vollständig spezifizierten Aufruf. Lies die verwandten Issues #54184, #56714 und #62680, um den bestehenden Inferenzkontext zu verstehen. Erledigt ist die Aufgabe, wenn beide Beispiele ohne angegebenes Trennzeichen mit dem erwarteten Literaltyp kompiliert werden, während der Fall mit angegebenem Trennzeichen weiterhin korrekt bleibt.
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
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100