microsoft / microsoft/TypeScript

Inconsistency involving discriminated union and flatMap

Offen
#63,776 4 Kommentare 1 Reaktion 2 zugewiesene Personen Auf GitHub ansehen

@RyanCavanaugh arbeitet bereits daran.

Seit 21.8.2026.

  • #3301 von @copilot-swe-agent — ohne Merge geschlossen
  • #63948 von @copilot-swe-agent — offen
Experimentation Needed
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

The following code works in TS, but errors in Go:

export type InputOp = { op: "add" } | { op: "remove"; value?: Array<unknown> };
export type OutputOp = { op: "add" | "remove" };

export function f(operations: InputOp[]): OutputOp[] {
  return operations.flatMap((operation) => {
    if (operation.op === "remove" && operation.value) {
      return [].map(() => ({ op: "remove" }));
    } else {
      return [operation];
    }
  });
}

The error in Go is:

src/flatMap.ts:5:29 - error TS2345: Argument of type '(this: undefined, operation: InputOp) => { op: "remove"; }[] | InputOp[]' is not assignable to parameter of type '(this: undefined, value: InputOp, index: number, array: InputOp[]) => readonly { op: "remove"; }[] | { op: "remove"; }'.
  Type '{ op: "remove"; }[] | InputOp[]' is not assignable to type 'readonly { op: "remove"; }[] | { op: "remove"; }'.
    Type 'InputOp[]' is not assignable to type 'readonly { op: "remove"; }[] | { op: "remove"; }'.
      Type 'InputOp[]' is not assignable to type 'readonly { op: "remove"; }[]'.
        Type 'InputOp' is not assignable to type '{ op: "remove"; }'.
          Type '{ op: "add"; }' is not assignable to type '{ op: "remove"; }'.
            Types of property 'op' are incompatible.
              Type '"add"' is not assignable to type '"remove"'.

5   return operations.flatMap((operation) => {
                              ~~~~~~~~~~~~~~~~

The example is seemingly quite close to minimal: removing the && operation.value, replacing the first case with [{ op: "remove" }], removing either case, replacing flatMap with map, or adding more explicit annotations, all either make TS error or make Go pass. I guess there's some complicated interaction of how Go is inferring the type of the callback when narrowing? Or maybe those things all let the inference happen some other way that works better, avoiding the problematic case.

(This is simplified from some real code which is a bit more complex, of course it doesn't make sense as-is.)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.