microsoft / microsoft/TypeScript
flatMap errors when callback returns certain union types
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
TypeScript Version: 3.4.3
Search Terms: flat, flatmap
Code
// Map<string, number[]>
const myMap = new Map([
['foo', [1, 2, 3]],
['bar', [4, 5, 6]]
]);
// string[]
const myArray = ['foo', 'bar', 'baz'];
// (key: any) => number[] | undefined
const mapFn = key => myMap.get(key);
const flatMapped = myArray.flatMap(mapFn);
Actual behavior:
With strict null checks on:
Argument of type '(key: any) => number[] | undefined' is not assignable to parameter of type '(this: undefined, value: string, index: number, array: string[]) => number | readonly number[]'.
Type 'number[] | undefined' is not assignable to type 'number | readonly number[]'.
Type 'undefined' is not assignable to type 'number | readonly number[]'. ts(2345)
Turning strict null checks off prevents the error as you might expect, but that's obviously not desirable.
More generally, any callback that returns foo[] | bar (where bar is not compatible with foo) seems to run into the same issue. The Map.prototype.get example I've used here just seems like it would be a relatively common use case in particular (you'd typically follow it up by calling .filter(Boolean) or similar on the mapped result).
More code
Seeing if doing the operations separately will help:
// (number[] | undefined)[]
const mapped = myArray.map(mapFn);
// any[] !!
const flattened = mapped.flat();
Expected behavior:
flatMapped/flattened result to be inferred as (number | undefined)[].
Playground Link:
Doesn't seem to be able to load the appropriate library definition, even with /// <reference lib="es2019.array" />
Related Issues:
#29604 seems to be related to the .flat() part.
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
Das TypeScript 3.4.3-Beispiel mit aktivierten strict null checks reproduzieren und dabei flatMap(mapFn) mit den separaten Aufrufen von map und flat vergleichen. Die Bibliotheksdefinition und das Type-Checking-Verhalten für Callbacks verfolgen, die number[] | undefined zurückgeben; abgeschlossen ist die Aufgabe, wenn das gemeldete Beispiel den Callback akzeptiert und wie erwartet (number | undefined)[] ableitet.
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
- 35/100