microsoft / microsoft/TypeScript
`ReturnType` and `InstanceType` don't work for functions/constructors with `never` in their parameters
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 Search Terms
ReturnType InstanceType any never
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about
ReturnTypeandInstanceType.
⏯ Playground Link
💻 Code
type _1 = ReturnType<(...args: never) => 'foo'>
// ^? type _1 = any
type _2 = InstanceType<new (...args: never) => 'foo'>
// ^? type _2 = any
🙁 Actual behavior
ReturnType and InstanceType always evaluate to any when given a function/constructor whose parameters are ...args: never (or ...args: never[], a: never, b: string, etc).
🙂 Expected behavior
ReturnType and InstanceType should return the actual return/instance type.
Additional information about the issue
Changing the parameter constraint in the conditions of these types from any to never seems to fix the issue:
type ReturnType<T extends (...args: any) => any> = T extends (...args: never) => infer R ? R : any
type InstanceType<T extends abstract new (...args: any) => any> = T extends abstract new (...args: never) => infer R ? R : any
I don't think this will cause any regressions, and did some spot-checking to validate that, but haven't run the entire test suite or anything like that yet.
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
Reproduziere die beiden Beispiele im verlinkten TypeScript Playground, suche dann die Deklarationen von ReturnType und InstanceType und vergleiche deren Einschränkungen für bedingte Parameter mit der vorgeschlagenen never-Form. Füge Testabdeckung für Funktionen und Konstruktoren mit never-Parametern hinzu und überprüfe, dass die inferierten Typen 'foo' statt any sind.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 42/100