Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25499,12 +25499,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function getSingleCommonSupertype(types: Type[]) {
// First, find the leftmost type for which no type to the right is a strict supertype, and if that
// type is a strict supertype of all other candidates, return it. Otherwise, return the leftmost type
// for which no type to the right is a (regular) supertype.
// type is a strict supertype of all other candidates, return it. Otherwise, use asymmetric
// assignability as a tiebreaker (i.e. only switch candidates when the assignment is one-way).
const candidate = reduceLeft(types, (s, t) => isTypeStrictSubtypeOf(s, t) ? t : s)!;
return every(types, t => t === candidate || isTypeStrictSubtypeOf(t, candidate)) ?
candidate :
reduceLeft(types, (s, t) => isTypeSubtypeOf(s, t) ? t : s)!;
reduceLeft(types, (s, t) => isTypeAssignableTo(s, t) && !isTypeAssignableTo(t, s) ? t : s)!;
}

// Return the leftmost type for which no type to the right is a subtype.
Expand Down
48 changes: 48 additions & 0 deletions tests/baselines/reference/discriminatedUnionFlatMap.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//// [tests/cases/compiler/discriminatedUnionFlatMap.ts] ////

=== discriminatedUnionFlatMap.ts ===
// https://github.com/microsoft/typescript-go/issues/1057

export type InputOp = { op: "add" } | { op: "remove"; value?: Array<unknown> };
>InputOp : Symbol(InputOp, Decl(discriminatedUnionFlatMap.ts, 0, 0))
>op : Symbol(op, Decl(discriminatedUnionFlatMap.ts, 2, 23))
>op : Symbol(op, Decl(discriminatedUnionFlatMap.ts, 2, 39))
>value : Symbol(value, Decl(discriminatedUnionFlatMap.ts, 2, 53))
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --) ... and 2 more)

export type OutputOp = { op: "add" | "remove" };
>OutputOp : Symbol(OutputOp, Decl(discriminatedUnionFlatMap.ts, 2, 79))
>op : Symbol(op, Decl(discriminatedUnionFlatMap.ts, 3, 24))

export function f(operations: InputOp[]): OutputOp[] {
>f : Symbol(f, Decl(discriminatedUnionFlatMap.ts, 3, 48))
>operations : Symbol(operations, Decl(discriminatedUnionFlatMap.ts, 5, 18))
>InputOp : Symbol(InputOp, Decl(discriminatedUnionFlatMap.ts, 0, 0))
>OutputOp : Symbol(OutputOp, Decl(discriminatedUnionFlatMap.ts, 2, 79))

return operations.flatMap((operation) => {
>operations.flatMap : Symbol(Array.flatMap, Decl(lib.es2019.array.d.ts, --, --))
>operations : Symbol(operations, Decl(discriminatedUnionFlatMap.ts, 5, 18))
>flatMap : Symbol(Array.flatMap, Decl(lib.es2019.array.d.ts, --, --))
>operation : Symbol(operation, Decl(discriminatedUnionFlatMap.ts, 6, 31))

if (operation.op === "remove" && operation.value) {
>operation.op : Symbol(op, Decl(discriminatedUnionFlatMap.ts, 2, 23), Decl(discriminatedUnionFlatMap.ts, 2, 39))
>operation : Symbol(operation, Decl(discriminatedUnionFlatMap.ts, 6, 31))
>op : Symbol(op, Decl(discriminatedUnionFlatMap.ts, 2, 23), Decl(discriminatedUnionFlatMap.ts, 2, 39))
>operation.value : Symbol(value, Decl(discriminatedUnionFlatMap.ts, 2, 53))
>operation : Symbol(operation, Decl(discriminatedUnionFlatMap.ts, 6, 31))
>value : Symbol(value, Decl(discriminatedUnionFlatMap.ts, 2, 53))

return [].map(() => ({ op: "remove" }));
>[].map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
>op : Symbol(op, Decl(discriminatedUnionFlatMap.ts, 8, 34))

} else {
return [operation];
>operation : Symbol(operation, Decl(discriminatedUnionFlatMap.ts, 6, 31))
}
});
}

91 changes: 91 additions & 0 deletions tests/baselines/reference/discriminatedUnionFlatMap.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//// [tests/cases/compiler/discriminatedUnionFlatMap.ts] ////

=== discriminatedUnionFlatMap.ts ===
// https://github.com/microsoft/typescript-go/issues/1057

export type InputOp = { op: "add" } | { op: "remove"; value?: Array<unknown> };
>InputOp : InputOp
> : ^^^^^^^
>op : "add"
> : ^^^^^
>op : "remove"
> : ^^^^^^^^
>value : unknown[] | undefined
> : ^^^^^^^^^^^^^^^^^^^^^

export type OutputOp = { op: "add" | "remove" };
>OutputOp : OutputOp
> : ^^^^^^^^
>op : "add" | "remove"
> : ^^^^^^^^^^^^^^^^

export function f(operations: InputOp[]): OutputOp[] {
>f : (operations: InputOp[]) => OutputOp[]
> : ^ ^^ ^^^^^
>operations : InputOp[]
> : ^^^^^^^^^

return operations.flatMap((operation) => {
>operations.flatMap((operation) => { if (operation.op === "remove" && operation.value) { return [].map(() => ({ op: "remove" })); } else { return [operation]; } }) : InputOp[]
> : ^^^^^^^^^
>operations.flatMap : <U, This = undefined>(callback: (this: This, value: InputOp, index: number, array: InputOp[]) => U | readonly U[], thisArg?: This | undefined) => U[]
> : ^ ^^ ^^^^^^^^^^^^^^ ^^^ ^^^^^^^^ ^^^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>operations : InputOp[]
> : ^^^^^^^^^
>flatMap : <U, This = undefined>(callback: (this: This, value: InputOp, index: number, array: InputOp[]) => U | readonly U[], thisArg?: This | undefined) => U[]
> : ^ ^^ ^^^^^^^^^^^^^^ ^^^ ^^^^^^^^ ^^^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>(operation) => { if (operation.op === "remove" && operation.value) { return [].map(() => ({ op: "remove" })); } else { return [operation]; } } : (this: undefined, operation: InputOp) => InputOp[] | { op: "remove"; }[]
> : ^ ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>operation : InputOp
> : ^^^^^^^

if (operation.op === "remove" && operation.value) {
>operation.op === "remove" && operation.value : false | unknown[] | undefined
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>operation.op === "remove" : boolean
> : ^^^^^^^
>operation.op : "add" | "remove"
> : ^^^^^^^^^^^^^^^^
>operation : InputOp
> : ^^^^^^^
>op : "add" | "remove"
> : ^^^^^^^^^^^^^^^^
>"remove" : "remove"
> : ^^^^^^^^
>operation.value : unknown[] | undefined
> : ^^^^^^^^^^^^^^^^^^^^^
>operation : { op: "remove"; value?: Array<unknown>; }
> : ^^^^^^ ^^^^^^^^^^ ^^^
>value : unknown[] | undefined
> : ^^^^^^^^^^^^^^^^^^^^^

return [].map(() => ({ op: "remove" }));
>[].map(() => ({ op: "remove" })) : { op: "remove"; }[]
> : ^^^^^^^^^^^^^^^^^^^
>[].map : <U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[]
> : ^ ^^ ^^^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^^^
>[] : never[]
> : ^^^^^^^
>map : <U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[]
> : ^ ^^ ^^^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^^^
>() => ({ op: "remove" }) : () => { op: "remove"; }
> : ^^^^^^^^^^^^^^^^^^^^^^^
>({ op: "remove" }) : { op: "remove"; }
> : ^^^^^^^^^^^^^^^^^
>{ op: "remove" } : { op: "remove"; }
> : ^^^^^^^^^^^^^^^^^
>op : "remove"
> : ^^^^^^^^
>"remove" : "remove"
> : ^^^^^^^^

} else {
return [operation];
>[operation] : InputOp[]
> : ^^^^^^^^^
>operation : InputOp
> : ^^^^^^^
}
});
}

18 changes: 18 additions & 0 deletions tests/cases/compiler/discriminatedUnionFlatMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @strict: true
// @noEmit: true
// @lib: es2019

// https://github.com/microsoft/typescript-go/issues/1057

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];
}
});
}
Loading