From 5d3ecaebadf7904825e7b9286ef03cf26a998a26 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 23 Dec 2017 19:12:49 -0800 Subject: [PATCH 1/3] Fix narrowTypeBySwitchOnDiscriminant function --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5b6aaa5dabf..f739a7d0a0b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12914,7 +12914,7 @@ namespace ts { const discriminantType = getUnionType(clauseTypes); const caseType = discriminantType.flags & TypeFlags.Never ? neverType : - replacePrimitivesWithLiterals(filterType(type, t => isTypeComparableTo(discriminantType, t)), discriminantType); + replacePrimitivesWithLiterals(filterType(type, t => areTypesComparable(discriminantType, t)), discriminantType); if (!hasDefaultClause) { return caseType; } From 319ad71b159fe9bab4056dc65a9a67a9b25d0742 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 23 Dec 2017 19:28:13 -0800 Subject: [PATCH 2/3] Add regression test --- .../switchWithConstrainedTypeVariable.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/cases/conformance/controlFlow/switchWithConstrainedTypeVariable.ts diff --git a/tests/cases/conformance/controlFlow/switchWithConstrainedTypeVariable.ts b/tests/cases/conformance/controlFlow/switchWithConstrainedTypeVariable.ts new file mode 100644 index 00000000000..f6efd36811e --- /dev/null +++ b/tests/cases/conformance/controlFlow/switchWithConstrainedTypeVariable.ts @@ -0,0 +1,14 @@ +// @strict: true + +// Repro from #20840 + +function function1(key: T) { + switch (key) { + case 'a': + key.toLowerCase(); + break; + default: + key.toLowerCase(); + break; + } +} From d1befefab9732301ab17ba011af4f7a00e76b536 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 23 Dec 2017 19:28:20 -0800 Subject: [PATCH 3/3] Accept new baselines --- .../switchWithConstrainedTypeVariable.js | 28 ++++++++++++++++ .../switchWithConstrainedTypeVariable.symbols | 29 ++++++++++++++++ .../switchWithConstrainedTypeVariable.types | 33 +++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 tests/baselines/reference/switchWithConstrainedTypeVariable.js create mode 100644 tests/baselines/reference/switchWithConstrainedTypeVariable.symbols create mode 100644 tests/baselines/reference/switchWithConstrainedTypeVariable.types diff --git a/tests/baselines/reference/switchWithConstrainedTypeVariable.js b/tests/baselines/reference/switchWithConstrainedTypeVariable.js new file mode 100644 index 00000000000..8e6bbe07fe1 --- /dev/null +++ b/tests/baselines/reference/switchWithConstrainedTypeVariable.js @@ -0,0 +1,28 @@ +//// [switchWithConstrainedTypeVariable.ts] +// Repro from #20840 + +function function1(key: T) { + switch (key) { + case 'a': + key.toLowerCase(); + break; + default: + key.toLowerCase(); + break; + } +} + + +//// [switchWithConstrainedTypeVariable.js] +"use strict"; +// Repro from #20840 +function function1(key) { + switch (key) { + case 'a': + key.toLowerCase(); + break; + default: + key.toLowerCase(); + break; + } +} diff --git a/tests/baselines/reference/switchWithConstrainedTypeVariable.symbols b/tests/baselines/reference/switchWithConstrainedTypeVariable.symbols new file mode 100644 index 00000000000..76a3e1c818f --- /dev/null +++ b/tests/baselines/reference/switchWithConstrainedTypeVariable.symbols @@ -0,0 +1,29 @@ +=== tests/cases/conformance/controlFlow/switchWithConstrainedTypeVariable.ts === +// Repro from #20840 + +function function1(key: T) { +>function1 : Symbol(function1, Decl(switchWithConstrainedTypeVariable.ts, 0, 0)) +>T : Symbol(T, Decl(switchWithConstrainedTypeVariable.ts, 2, 19)) +>key : Symbol(key, Decl(switchWithConstrainedTypeVariable.ts, 2, 40)) +>T : Symbol(T, Decl(switchWithConstrainedTypeVariable.ts, 2, 19)) + + switch (key) { +>key : Symbol(key, Decl(switchWithConstrainedTypeVariable.ts, 2, 40)) + + case 'a': + key.toLowerCase(); +>key.toLowerCase : Symbol(String.toLowerCase, Decl(lib.d.ts, --, --)) +>key : Symbol(key, Decl(switchWithConstrainedTypeVariable.ts, 2, 40)) +>toLowerCase : Symbol(String.toLowerCase, Decl(lib.d.ts, --, --)) + + break; + default: + key.toLowerCase(); +>key.toLowerCase : Symbol(String.toLowerCase, Decl(lib.d.ts, --, --)) +>key : Symbol(key, Decl(switchWithConstrainedTypeVariable.ts, 2, 40)) +>toLowerCase : Symbol(String.toLowerCase, Decl(lib.d.ts, --, --)) + + break; + } +} + diff --git a/tests/baselines/reference/switchWithConstrainedTypeVariable.types b/tests/baselines/reference/switchWithConstrainedTypeVariable.types new file mode 100644 index 00000000000..be7cc59f348 --- /dev/null +++ b/tests/baselines/reference/switchWithConstrainedTypeVariable.types @@ -0,0 +1,33 @@ +=== tests/cases/conformance/controlFlow/switchWithConstrainedTypeVariable.ts === +// Repro from #20840 + +function function1(key: T) { +>function1 : (key: T) => void +>T : T +>key : T +>T : T + + switch (key) { +>key : T + + case 'a': +>'a' : "a" + + key.toLowerCase(); +>key.toLowerCase() : string +>key.toLowerCase : () => string +>key : T +>toLowerCase : () => string + + break; + default: + key.toLowerCase(); +>key.toLowerCase() : string +>key.toLowerCase : () => string +>key : T +>toLowerCase : () => string + + break; + } +} +