From baac083a31cfc967ce722c385f000fc3f3b058ea Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 17 Dec 2019 13:09:58 -0800 Subject: [PATCH] Add tests --- .../types/union/unionTypeReduction2.ts | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 tests/cases/conformance/types/union/unionTypeReduction2.ts diff --git a/tests/cases/conformance/types/union/unionTypeReduction2.ts b/tests/cases/conformance/types/union/unionTypeReduction2.ts new file mode 100644 index 00000000000..ed456f0cb05 --- /dev/null +++ b/tests/cases/conformance/types/union/unionTypeReduction2.ts @@ -0,0 +1,66 @@ +// @strict: true + +function f1(x: { f(): void }, y: { f(x?: string): void }) { + let z = !!true ? x : y; // { f(x?: string): void } + z.f(); + z.f('hello'); +} + +function f2(x: { f(x: string | undefined): void }, y: { f(x?: string): void }) { + let z = !!true ? x : y; // { f(x?: string): void } + z.f(); + z.f('hello'); +} + +function f3(x: () => void, y: (x?: string) => void) { + let f = !!true ? x : y; // (x?: string) => void + f(); + f('hello'); +} + +function f4(x: (x: string | undefined) => void, y: (x?: string) => void) { + let f = !!true ? x : y; // (x?: string) => void + f(); + f('hello'); +} + +function f5(x: (x: string | undefined) => void, y: (x?: 'hello') => void) { + let f = !!true ? x : y; // (x?: 'hello') => void + f(); + f('hello'); +} + +function f6(x: (x: 'hello' | undefined) => void, y: (x?: string) => void) { + let f = !!true ? x : y; // (x: 'hello' | undefined) => void + f(); // Error + f('hello'); +} + +type A = { + f(): void; +} + +type B = { + f(x?: string): void; + g(): void; +} + +function f11(a: A, b: B) { + let z = !!true ? a : b; // A | B + z.f(); + z.f('hello'); +} + +// Repro from #35414 + +interface ReturnVal { + something(): void; +} + +const k: ReturnVal = { something() { } } + +declare const val: ReturnVal; +function run(options: { something?(b?: string): void }) { + const something = options.something ?? val.something; + something(''); +}