microsoft / microsoft/TypeScript
Inconsistency involving discriminated union and flatMap
@RyanCavanaugh đang làm issue này rồi.
Từ ngày 21/8/2026.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.3k
- Merge trung bình
- 2 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 132
Mô tả
The following code works in TS, but errors in Go:
```ts
export type InputOp = { op: "add" } | { op: "remove"; value?: Array };
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:
```ts
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.)
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Đánh giá
Issue này chưa được đánh giá.