microsoft / microsoft/TypeScript
Function expressions widen to `{}` in auto arrays
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
function expression widen widening auto array any empty objecr
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 Code
const arr1 = [];
arr1.push(function () {
return "";
});
arr1;
// ^? const arr1: {}[]
const arr2 = [];
arr2.push(() => {
return "";
});
arr2;
// ^? const arr2: (() => string)[]
🙁 Actual behavior
arr1's type after this .push is {}[]
🙂 Expected behavior
I'd expect the type at both of the checked locations to be the same ((() => string)[])
Additional information about the issue
Likely the difference comes from the fact that the function expression is context-sensitive - maybe this is just a design limitation. However, the same doesn't happen in JS with expando objects (TS playground):
const obj = {}
obj.foo = function () {
return ""
}
const result = obj.foo
// ^? const result: () => string
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 dem verknüpften TypeScript Playground-Beispiel und vergleiche die abgeleiteten Typen von arr1 und arr2 nach den jeweiligen push-Aufrufen. Verwende das zusätzliche JavaScript-expando-object-Beispiel als Vergleichspunkt; abgeschlossen ist die Aufgabe, wenn die Fälle mit function-expression und arrow-function konsistente abgeleitete Array-Elementtypen haben und eine Regressionstestabdeckung für das gemeldete Verhalten vorhanden ist.
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
- Klar beschrieben
- Anfängerfreundlichkeit
- 35/100