microsoft / microsoft/TypeScript
Adding generic parameter to a lambda function removes typing from the function argument
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
TypeScript Version: 3.2.2
Search Terms: generic removes parameter type
Code
// OKAY
// declare const labmda function with typings, then implement with minimal type annotations.
// "input" has type of "string".
export const foo: (input: string) => string
= (input) => (input);
// OKAY
// declare const lambda generic function without typings, then implement with full inline type annotations.
// "input" has type of "string".
export const bar
= <TResult>(input: string) => ({ } as Partial<TResult>);
// ERROR
// declare const labmda generic function with typings, then implement with minimal type annotations.
// "input" has type of "any".
export const baz: <TResult>(input: string) => Partial<TResult>
= <TResult>(input) => ({ [input]: input } as Partial<TResult>);
Expected behavior:
input is typed as string.
Actual behavior:
input is typed as any.
(NOTE: options -> noImplicitAny to have the issue highlighted as an error.)
Related Issues:
https://github.com/Microsoft/TypeScript/issues/18606
I think this issue might be related, but I don't understand it enough to judge.
If this is the same issue then the answer is "this is correct, and a limitation of the type system", which is fair enough.
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, das Beispiel im verlinkten TypeScript Playground mit aktiviertem noImplicitAny zu reproduzieren, und vergleiche anschließend die drei Lambda-Deklarationen sowie das zugehörige Issue #18606. Verfolge, wie die kontextbezogene Typisierung auf die Zuweisung der generischen Lambda angewendet wird. Als abgeschlossen gilt die Aufgabe, wenn festgestellt wurde, ob das Argument als string inferiert werden sollte, und bei bestätigtem Bug ein Regressionstest hinzugefügt wurde.
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
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 38/100