Skip to content

In switch case, narrow base constraint of type #21483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
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
1 change: 1 addition & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12666,6 +12666,7 @@ namespace ts {
if (!switchTypes.length) {
return type;
}
type = getBaseConstraintOfType(type) || type;
const clauseTypes = switchTypes.slice(clauseStart, clauseEnd);
const hasDefaultClause = clauseStart === clauseEnd || contains(clauseTypes, neverType);
const discriminantType = getUnionType(clauseTypes);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
tests/cases/conformance/controlFlow/switchWithConstrainedTypeVariable.ts(22,19): error TS2322: Type '"a"' is not assignable to type 'never'.
tests/cases/conformance/controlFlow/switchWithConstrainedTypeVariable.ts(23,19): error TS2322: Type '"b"' is not assignable to type 'never'.
tests/cases/conformance/controlFlow/switchWithConstrainedTypeVariable.ts(24,19): error TS2322: Type '"c"' is not assignable to type 'never'.


==== tests/cases/conformance/controlFlow/switchWithConstrainedTypeVariable.ts (3 errors) ====
// Repro from #20840

function function1<T extends 'a' | 'b'>(key: T) {
switch (key) {
case 'a':
key.toLowerCase();
break;
default:
key.toLowerCase();
break;
}
}

// #20375

declare var n: never;
declare function never(never: never): never;
function f<T extends 'a' | 'b' | 'c'>(t: T): void {
switch (t) {
// in a/b/c branches, assignment should fail as t narrows to a/b/c
// in default branch, assignment should be fine
case 'a': n = t; break;
~
!!! error TS2322: Type '"a"' is not assignable to type 'never'.
case 'b': n = t; break;
~
!!! error TS2322: Type '"b"' is not assignable to type 'never'.
case 'c': n = t; break;
~
!!! error TS2322: Type '"c"' is not assignable to type 'never'.
default: n = t; break;
}
}


34 changes: 34 additions & 0 deletions tests/baselines/reference/switchWithConstrainedTypeVariable.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ function function1<T extends 'a' | 'b'>(key: T) {
break;
}
}

// #20375

declare var n: never;
declare function never(never: never): never;
function f<T extends 'a' | 'b' | 'c'>(t: T): void {
switch (t) {
// in a/b/c branches, assignment should fail as t narrows to a/b/c
// in default branch, assignment should be fine
case 'a': n = t; break;
case 'b': n = t; break;
case 'c': n = t; break;
default: n = t; break;
}
}



//// [switchWithConstrainedTypeVariable.js]
Expand All @@ -26,3 +42,21 @@ function function1(key) {
break;
}
}
function f(t) {
switch (t) {
// in a/b/c branches, assignment should fail as t narrows to a/b/c
// in default branch, assignment should be fine
case 'a':
n = t;
break;
case 'b':
n = t;
break;
case 'c':
n = t;
break;
default:
n = t;
break;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,42 @@ function function1<T extends 'a' | 'b'>(key: T) {
}
}

// #20375

declare var n: never;
>n : Symbol(n, Decl(switchWithConstrainedTypeVariable.ts, 15, 11))

declare function never(never: never): never;
>never : Symbol(never, Decl(switchWithConstrainedTypeVariable.ts, 15, 21))
>never : Symbol(never, Decl(switchWithConstrainedTypeVariable.ts, 16, 23))

function f<T extends 'a' | 'b' | 'c'>(t: T): void {
>f : Symbol(f, Decl(switchWithConstrainedTypeVariable.ts, 16, 44))
>T : Symbol(T, Decl(switchWithConstrainedTypeVariable.ts, 17, 11))
>t : Symbol(t, Decl(switchWithConstrainedTypeVariable.ts, 17, 38))
>T : Symbol(T, Decl(switchWithConstrainedTypeVariable.ts, 17, 11))

switch (t) {
>t : Symbol(t, Decl(switchWithConstrainedTypeVariable.ts, 17, 38))

// in a/b/c branches, assignment should fail as t narrows to a/b/c
// in default branch, assignment should be fine
case 'a': n = t; break;
>n : Symbol(n, Decl(switchWithConstrainedTypeVariable.ts, 15, 11))
>t : Symbol(t, Decl(switchWithConstrainedTypeVariable.ts, 17, 38))

case 'b': n = t; break;
>n : Symbol(n, Decl(switchWithConstrainedTypeVariable.ts, 15, 11))
>t : Symbol(t, Decl(switchWithConstrainedTypeVariable.ts, 17, 38))

case 'c': n = t; break;
>n : Symbol(n, Decl(switchWithConstrainedTypeVariable.ts, 15, 11))
>t : Symbol(t, Decl(switchWithConstrainedTypeVariable.ts, 17, 38))

default: n = t; break;
>n : Symbol(n, Decl(switchWithConstrainedTypeVariable.ts, 15, 11))
>t : Symbol(t, Decl(switchWithConstrainedTypeVariable.ts, 17, 38))
}
}


50 changes: 48 additions & 2 deletions tests/baselines/reference/switchWithConstrainedTypeVariable.types
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,64 @@ function function1<T extends 'a' | 'b'>(key: T) {
key.toLowerCase();
>key.toLowerCase() : string
>key.toLowerCase : () => string
>key : T
>key : "a"
>toLowerCase : () => string

break;
default:
key.toLowerCase();
>key.toLowerCase() : string
>key.toLowerCase : () => string
>key : T
>key : "b"
>toLowerCase : () => string

break;
}
}

// #20375

declare var n: never;
>n : never

declare function never(never: never): never;
>never : (never: never) => never
>never : never

function f<T extends 'a' | 'b' | 'c'>(t: T): void {
>f : <T extends "a" | "b" | "c">(t: T) => void
>T : T
>t : T
>T : T

switch (t) {
>t : T

// in a/b/c branches, assignment should fail as t narrows to a/b/c
// in default branch, assignment should be fine
case 'a': n = t; break;
>'a' : "a"
>n = t : "a"
>n : never
>t : "a"

case 'b': n = t; break;
>'b' : "b"
>n = t : "b"
>n : never
>t : "b"

case 'c': n = t; break;
>'c' : "c"
>n = t : "c"
>n : never
>t : "c"

default: n = t; break;
>n = t : never
>n : never
>t : never
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,19 @@ function function1<T extends 'a' | 'b'>(key: T) {
break;
}
}

// #20375

declare var n: never;
declare function never(never: never): never;
function f<T extends 'a' | 'b' | 'c'>(t: T): void {
switch (t) {
// in a/b/c branches, assignment should fail as t narrows to a/b/c
// in default branch, assignment should be fine
case 'a': n = t; break;
case 'b': n = t; break;
case 'c': n = t; break;
default: n = t; break;
}
}