From 300cbef07105247ec1c826de9d785bfc72849103 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 22 May 2019 17:42:05 -0700 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20crash=20when=20creating=20a=20u?= =?UTF-8?q?nion=20signature=20from=20signatures=20that=20do=20and=20don?= =?UTF-8?q?=E2=80=99t=20have=20this=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/compiler/checker.ts | 6 ++-- .../reference/unionTypeCallSignatures5.js | 17 ++++++++++ .../unionTypeCallSignatures5.symbols | 31 +++++++++++++++++++ .../reference/unionTypeCallSignatures5.types | 24 ++++++++++++++ .../types/union/unionTypeCallSignatures5.ts | 12 +++++++ 5 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/unionTypeCallSignatures5.js create mode 100644 tests/baselines/reference/unionTypeCallSignatures5.symbols create mode 100644 tests/baselines/reference/unionTypeCallSignatures5.types create mode 100644 tests/cases/conformance/types/union/unionTypeCallSignatures5.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 05b1015b12428..936c73ee9a041 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7052,10 +7052,10 @@ namespace ts { // Union the result types when more than one signature matches if (unionSignatures.length > 1) { let thisParameter = signature.thisParameter; - if (forEach(unionSignatures, sig => sig.thisParameter)) { - // TODO: GH#18217 We tested that *some* has thisParameter and now act as if *all* do + const firstThisParameterOfUnionSignatures = forEach(unionSignatures, sig => sig.thisParameter); + if (firstThisParameterOfUnionSignatures) { const thisType = getUnionType(map(unionSignatures, sig => sig.thisParameter ? getTypeOfSymbol(sig.thisParameter) : anyType), UnionReduction.Subtype); - thisParameter = createSymbolWithType(signature.thisParameter!, thisType); + thisParameter = createSymbolWithType(firstThisParameterOfUnionSignatures, thisType); } s = createUnionSignature(signature, unionSignatures); s.thisParameter = thisParameter; diff --git a/tests/baselines/reference/unionTypeCallSignatures5.js b/tests/baselines/reference/unionTypeCallSignatures5.js new file mode 100644 index 0000000000000..2e756540d9437 --- /dev/null +++ b/tests/baselines/reference/unionTypeCallSignatures5.js @@ -0,0 +1,17 @@ +//// [unionTypeCallSignatures5.ts] +// #31485 +interface A { + (this: void, b?: number): void; +} +interface B { + (this: number, b?: number): void; +} +interface C { + (i: number): void; +} +declare const fn: A | B | C; +fn(0); + + +//// [unionTypeCallSignatures5.js] +fn(0); diff --git a/tests/baselines/reference/unionTypeCallSignatures5.symbols b/tests/baselines/reference/unionTypeCallSignatures5.symbols new file mode 100644 index 0000000000000..d729dcd7aba6d --- /dev/null +++ b/tests/baselines/reference/unionTypeCallSignatures5.symbols @@ -0,0 +1,31 @@ +=== tests/cases/conformance/types/union/unionTypeCallSignatures5.ts === +// #31485 +interface A { +>A : Symbol(A, Decl(unionTypeCallSignatures5.ts, 0, 0)) + + (this: void, b?: number): void; +>this : Symbol(this, Decl(unionTypeCallSignatures5.ts, 2, 3)) +>b : Symbol(b, Decl(unionTypeCallSignatures5.ts, 2, 14)) +} +interface B { +>B : Symbol(B, Decl(unionTypeCallSignatures5.ts, 3, 1)) + + (this: number, b?: number): void; +>this : Symbol(this, Decl(unionTypeCallSignatures5.ts, 5, 3)) +>b : Symbol(b, Decl(unionTypeCallSignatures5.ts, 5, 16)) +} +interface C { +>C : Symbol(C, Decl(unionTypeCallSignatures5.ts, 6, 1)) + + (i: number): void; +>i : Symbol(i, Decl(unionTypeCallSignatures5.ts, 8, 3)) +} +declare const fn: A | B | C; +>fn : Symbol(fn, Decl(unionTypeCallSignatures5.ts, 10, 13)) +>A : Symbol(A, Decl(unionTypeCallSignatures5.ts, 0, 0)) +>B : Symbol(B, Decl(unionTypeCallSignatures5.ts, 3, 1)) +>C : Symbol(C, Decl(unionTypeCallSignatures5.ts, 6, 1)) + +fn(0); +>fn : Symbol(fn, Decl(unionTypeCallSignatures5.ts, 10, 13)) + diff --git a/tests/baselines/reference/unionTypeCallSignatures5.types b/tests/baselines/reference/unionTypeCallSignatures5.types new file mode 100644 index 0000000000000..53ce4ea655086 --- /dev/null +++ b/tests/baselines/reference/unionTypeCallSignatures5.types @@ -0,0 +1,24 @@ +=== tests/cases/conformance/types/union/unionTypeCallSignatures5.ts === +// #31485 +interface A { + (this: void, b?: number): void; +>this : void +>b : number +} +interface B { + (this: number, b?: number): void; +>this : number +>b : number +} +interface C { + (i: number): void; +>i : number +} +declare const fn: A | B | C; +>fn : A | B | C + +fn(0); +>fn(0) : void +>fn : A | B | C +>0 : 0 + diff --git a/tests/cases/conformance/types/union/unionTypeCallSignatures5.ts b/tests/cases/conformance/types/union/unionTypeCallSignatures5.ts new file mode 100644 index 0000000000000..30a5ac031adc1 --- /dev/null +++ b/tests/cases/conformance/types/union/unionTypeCallSignatures5.ts @@ -0,0 +1,12 @@ +// #31485 +interface A { + (this: void, b?: number): void; +} +interface B { + (this: number, b?: number): void; +} +interface C { + (i: number): void; +} +declare const fn: A | B | C; +fn(0);