Skip to content

Commit eadb9e1

Browse files
authored
Don't generalize when assigment target is never (fix: #41707) (#59774)
1 parent 342d142 commit eadb9e1

30 files changed

+197
-78
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22056,8 +22056,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2205622056
const [sourceType, targetType] = getTypeNamesForErrorDisplay(source, target);
2205722057
let generalizedSource = source;
2205822058
let generalizedSourceType = sourceType;
22059-
22060-
if (isLiteralType(source) && !typeCouldHaveTopLevelSingletonTypes(target)) {
22059+
22060+
// Don't generalize on 'never' - we really want the original type
22061+
// to be displayed for use-cases like 'assertNever'.
22062+
if (!(target.flags & TypeFlags.Never) && isLiteralType(source) && !typeCouldHaveTopLevelSingletonTypes(target)) {
2206122063
generalizedSource = getBaseTypeOfLiteralType(source);
2206222064
Debug.assert(!isTypeAssignableTo(generalizedSource, target), "generalized source shouldn't be assignable");
2206322065
generalizedSourceType = getTypeNameForErrorDisplay(generalizedSource);

tests/baselines/reference/controlFlowArrayErrors.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ controlFlowArrayErrors.ts(19,9): error TS7034: Variable 'x' implicitly has type
66
controlFlowArrayErrors.ts(22,9): error TS7005: Variable 'x' implicitly has an 'any[]' type.
77
controlFlowArrayErrors.ts(29,12): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'.
88
controlFlowArrayErrors.ts(34,12): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'.
9-
controlFlowArrayErrors.ts(48,12): error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
9+
controlFlowArrayErrors.ts(48,12): error TS2345: Argument of type '99' is not assignable to parameter of type 'never'.
1010
controlFlowArrayErrors.ts(56,12): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
1111
controlFlowArrayErrors.ts(60,11): error TS7034: Variable 'x' implicitly has type 'any[]' in some locations where its type cannot be determined.
1212
controlFlowArrayErrors.ts(63,9): error TS7005: Variable 'x' implicitly has an 'any[]' type.
@@ -78,7 +78,7 @@ controlFlowArrayErrors.ts(63,9): error TS7005: Variable 'x' implicitly has an 'a
7878
x; // boolean[] | (string | number)[]
7979
x.push(99); // Error
8080
~~
81-
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
81+
!!! error TS2345: Argument of type '99' is not assignable to parameter of type 'never'.
8282
}
8383

8484
function f7() {

tests/baselines/reference/dependentDestructuredVariables.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
dependentDestructuredVariables.ts(334,5): error TS7022: 'value1' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
22
dependentDestructuredVariables.ts(334,5): error TS7031: Binding element 'value1' implicitly has an 'any' type.
3-
dependentDestructuredVariables.ts(431,15): error TS2322: Type 'number' is not assignable to type 'never'.
3+
dependentDestructuredVariables.ts(431,15): error TS2322: Type '1' is not assignable to type 'never'.
44

55

66
==== dependentDestructuredVariables.ts (3 errors) ====
@@ -440,7 +440,7 @@ dependentDestructuredVariables.ts(431,15): error TS2322: Type 'number' is not as
440440
if (y === undefined) {
441441
const shouldNotBeOk: never = x; // Error
442442
~~~~~~~~~~~~~
443-
!!! error TS2322: Type 'number' is not assignable to type 'never'.
443+
!!! error TS2322: Type '1' is not assignable to type 'never'.
444444
}
445445
}
446446

tests/baselines/reference/errorOnFunctionReturnType.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ foo.js(16,60): error TS2355: A function whose declared type is neither 'undefine
55
foo.js(21,20): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
66
foo.js(31,10): error TS2534: A function returning 'never' cannot have a reachable end point.
77
foo.js(35,12): error TS1065: The return type of an async function or method must be the global Promise<T> type.
8-
foo.js(37,5): error TS2322: Type 'string' is not assignable to type 'never'.
8+
foo.js(37,5): error TS2322: Type '"asd"' is not assignable to type 'never'.
99
foo.js(40,56): error TS2534: A function returning 'never' cannot have a reachable end point.
1010
foo.js(45,18): error TS2534: A function returning 'never' cannot have a reachable end point.
1111

@@ -64,7 +64,7 @@ foo.js(45,18): error TS2534: A function returning 'never' cannot have a reachabl
6464
async function testNever2() {
6565
return "asd";
6666
~~~~~~
67-
!!! error TS2322: Type 'string' is not assignable to type 'never'.
67+
!!! error TS2322: Type '"asd"' is not assignable to type 'never'.
6868
}
6969

7070
var testNever3 = /** @type {FunctionReturningNever} */ function() {

tests/baselines/reference/exhaustiveSwitchCheckCircularity.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
exhaustiveSwitchCheckCircularity.ts(14,26): error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
1+
exhaustiveSwitchCheckCircularity.ts(14,26): error TS2345: Argument of type '"bbb"' is not assignable to parameter of type 'never'.
22

33

44
==== exhaustiveSwitchCheckCircularity.ts (1 errors) ====
@@ -17,7 +17,7 @@ exhaustiveSwitchCheckCircularity.ts(14,26): error TS2345: Argument of type 'stri
1717
}
1818
else if (isNever(foo)) { // Error expected
1919
~~~
20-
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
20+
!!! error TS2345: Argument of type '"bbb"' is not assignable to parameter of type 'never'.
2121
break;
2222
}
2323
}

tests/baselines/reference/extractInferenceImprovement.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
extractInferenceImprovement.ts(26,26): error TS2345: Argument of type 'typeof s' is not assignable to parameter of type 'never'.
1+
extractInferenceImprovement.ts(26,26): error TS2345: Argument of type 'unique symbol' is not assignable to parameter of type 'never'.
22
extractInferenceImprovement.ts(28,1): error TS2322: Type 'string | number' is not assignable to type 'string'.
33
Type 'number' is not assignable to type 'string'.
44
extractInferenceImprovement.ts(28,26): error TS2345: Argument of type 'unique symbol' is not assignable to parameter of type '"first" | "second"'.
@@ -32,7 +32,7 @@ extractInferenceImprovement.ts(28,26): error TS2345: Argument of type 'unique sy
3232
// Should fail
3333
prop = getProperty2(obj, s);
3434
~
35-
!!! error TS2345: Argument of type 'typeof s' is not assignable to parameter of type 'never'.
35+
!!! error TS2345: Argument of type 'unique symbol' is not assignable to parameter of type 'never'.
3636

3737
prop = getProperty3(obj, s);
3838
~~~~

tests/baselines/reference/functionCallOnConstrainedTypeVariable.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
functionCallOnConstrainedTypeVariable.ts(11,7): error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
2-
functionCallOnConstrainedTypeVariable.ts(15,7): error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
3-
functionCallOnConstrainedTypeVariable.ts(18,5): error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
1+
functionCallOnConstrainedTypeVariable.ts(11,7): error TS2345: Argument of type '"s"' is not assignable to parameter of type 'never'.
2+
functionCallOnConstrainedTypeVariable.ts(15,7): error TS2345: Argument of type '"s"' is not assignable to parameter of type 'never'.
3+
functionCallOnConstrainedTypeVariable.ts(18,5): error TS2345: Argument of type '""' is not assignable to parameter of type 'never'.
44
functionCallOnConstrainedTypeVariable.ts(19,9): error TS2554: Expected 1 arguments, but got 4.
55

66

@@ -17,18 +17,18 @@ functionCallOnConstrainedTypeVariable.ts(19,9): error TS2554: Expected 1 argumen
1717
function call0(p: A | B) {
1818
p.a("s"); // Error
1919
~~~
20-
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
20+
!!! error TS2345: Argument of type '"s"' is not assignable to parameter of type 'never'.
2121
}
2222

2323
function callN<T extends A | B>(p: T) {
2424
p.a("s"); // Error
2525
~~~
26-
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
26+
!!! error TS2345: Argument of type '"s"' is not assignable to parameter of type 'never'.
2727

2828
var a: T["a"] = p.a;
2929
a(""); // Error
3030
~~
31-
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
31+
!!! error TS2345: Argument of type '""' is not assignable to parameter of type 'never'.
3232
a("", "", "", ""); // Error
3333
~~~~~~~~~~
3434
!!! error TS2554: Expected 1 arguments, but got 4.

tests/baselines/reference/keyofAndIndexedAccess2.errors.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
keyofAndIndexedAccess2.ts(4,5): error TS2322: Type 'string' is not assignable to type 'number'.
22
keyofAndIndexedAccess2.ts(6,5): error TS2322: Type '2' is not assignable to type '0 | 1'.
33
keyofAndIndexedAccess2.ts(7,5): error TS2322: Type '"x"' is not assignable to type '0 | 1'.
4-
keyofAndIndexedAccess2.ts(8,5): error TS2322: Type 'number' is not assignable to type 'never'.
5-
keyofAndIndexedAccess2.ts(9,5): error TS2322: Type 'number' is not assignable to type 'never'.
6-
keyofAndIndexedAccess2.ts(10,5): error TS2322: Type 'string' is not assignable to type 'never'.
4+
keyofAndIndexedAccess2.ts(8,5): error TS2322: Type '1' is not assignable to type 'never'.
5+
keyofAndIndexedAccess2.ts(9,5): error TS2322: Type '2' is not assignable to type 'never'.
6+
keyofAndIndexedAccess2.ts(10,5): error TS2322: Type '"x"' is not assignable to type 'never'.
77
keyofAndIndexedAccess2.ts(14,5): error TS2739: Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y
88
keyofAndIndexedAccess2.ts(15,5): error TS2322: Type 'T' is not assignable to type '{ x: number; y: number; }'.
99
Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y
@@ -18,7 +18,7 @@ keyofAndIndexedAccess2.ts(31,5): error TS2322: Type '{ [key: string]: number; }'
1818
keyofAndIndexedAccess2.ts(38,5): error TS2322: Type '{ [x: string]: number; }' is not assignable to type '{ [P in K]: number; }'.
1919
keyofAndIndexedAccess2.ts(50,3): error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Item'.
2020
No index signature with a parameter of type 'string' was found on type 'Item'.
21-
keyofAndIndexedAccess2.ts(51,3): error TS2322: Type 'number' is not assignable to type 'never'.
21+
keyofAndIndexedAccess2.ts(51,3): error TS2322: Type '123' is not assignable to type 'never'.
2222
keyofAndIndexedAccess2.ts(52,3): error TS2322: Type 'number' is not assignable to type 'T[keyof T]'.
2323
'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'number'.
2424
keyofAndIndexedAccess2.ts(53,3): error TS2322: Type 'number' is not assignable to type 'T[K]'.
@@ -30,7 +30,7 @@ keyofAndIndexedAccess2.ts(67,3): error TS2322: Type 'number' is not assignable t
3030
keyofAndIndexedAccess2.ts(68,3): error TS2322: Type 'number' is not assignable to type 'T[K]'.
3131
'number' is assignable to the constraint of type 'T[K]', but 'T[K]' could be instantiated with a different subtype of constraint 'number'.
3232
keyofAndIndexedAccess2.ts(108,5): error TS2322: Type '123' is not assignable to type 'Type[K]'.
33-
Type 'number' is not assignable to type 'never'.
33+
Type '123' is not assignable to type 'never'.
3434

3535

3636
==== keyofAndIndexedAccess2.ts (23 errors) ====
@@ -49,13 +49,13 @@ keyofAndIndexedAccess2.ts(108,5): error TS2322: Type '123' is not assignable to
4949
!!! error TS2322: Type '"x"' is not assignable to type '0 | 1'.
5050
obj[k2] = 1; // Error
5151
~~~~~~~
52-
!!! error TS2322: Type 'number' is not assignable to type 'never'.
52+
!!! error TS2322: Type '1' is not assignable to type 'never'.
5353
obj[k2] = 2; // Error
5454
~~~~~~~
55-
!!! error TS2322: Type 'number' is not assignable to type 'never'.
55+
!!! error TS2322: Type '2' is not assignable to type 'never'.
5656
obj[k2] = 'x'; // Error
5757
~~~~~~~
58-
!!! error TS2322: Type 'string' is not assignable to type 'never'.
58+
!!! error TS2322: Type '"x"' is not assignable to type 'never'.
5959
}
6060

6161
function f2<T extends { [key: string]: number }>(a: { x: number, y: number }, b: { [key: string]: number }, c: T, k: keyof T) {
@@ -121,7 +121,7 @@ keyofAndIndexedAccess2.ts(108,5): error TS2322: Type '123' is not assignable to
121121
!!! error TS7053: No index signature with a parameter of type 'string' was found on type 'Item'.
122122
obj[k2] = 123; // Error
123123
~~~~~~~
124-
!!! error TS2322: Type 'number' is not assignable to type 'never'.
124+
!!! error TS2322: Type '123' is not assignable to type 'never'.
125125
obj[k3] = 123; // Error
126126
~~~~~~~
127127
!!! error TS2322: Type 'number' is not assignable to type 'T[keyof T]'.
@@ -197,7 +197,7 @@ keyofAndIndexedAccess2.ts(108,5): error TS2322: Type '123' is not assignable to
197197
return 123; // Error
198198
~~~~~~
199199
!!! error TS2322: Type '123' is not assignable to type 'Type[K]'.
200-
!!! error TS2322: Type 'number' is not assignable to type 'never'.
200+
!!! error TS2322: Type '123' is not assignable to type 'never'.
201201
}
202202

203203
// Repro from #30920

tests/baselines/reference/logicalAssignment6(target=es2015).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
logicalAssignment6.ts(10,5): error TS2532: Object is possibly 'undefined'.
2-
logicalAssignment6.ts(10,42): error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
2+
logicalAssignment6.ts(10,42): error TS2345: Argument of type '100' is not assignable to parameter of type 'never'.
33

44

55
==== logicalAssignment6.ts (2 errors) ====
@@ -16,6 +16,6 @@ logicalAssignment6.ts(10,42): error TS2345: Argument of type 'number' is not ass
1616
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1717
!!! error TS2532: Object is possibly 'undefined'.
1818
~~~
19-
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
19+
!!! error TS2345: Argument of type '100' is not assignable to parameter of type 'never'.
2020
}
2121

tests/baselines/reference/logicalAssignment6(target=es2020).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
logicalAssignment6.ts(10,5): error TS2532: Object is possibly 'undefined'.
2-
logicalAssignment6.ts(10,42): error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
2+
logicalAssignment6.ts(10,42): error TS2345: Argument of type '100' is not assignable to parameter of type 'never'.
33

44

55
==== logicalAssignment6.ts (2 errors) ====
@@ -16,6 +16,6 @@ logicalAssignment6.ts(10,42): error TS2345: Argument of type 'number' is not ass
1616
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1717
!!! error TS2532: Object is possibly 'undefined'.
1818
~~~
19-
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
19+
!!! error TS2345: Argument of type '100' is not assignable to parameter of type 'never'.
2020
}
2121

tests/baselines/reference/logicalAssignment6(target=es2021).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
logicalAssignment6.ts(10,5): error TS2532: Object is possibly 'undefined'.
2-
logicalAssignment6.ts(10,42): error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
2+
logicalAssignment6.ts(10,42): error TS2345: Argument of type '100' is not assignable to parameter of type 'never'.
33

44

55
==== logicalAssignment6.ts (2 errors) ====
@@ -16,6 +16,6 @@ logicalAssignment6.ts(10,42): error TS2345: Argument of type 'number' is not ass
1616
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1717
!!! error TS2532: Object is possibly 'undefined'.
1818
~~~
19-
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
19+
!!! error TS2345: Argument of type '100' is not assignable to parameter of type 'never'.
2020
}
2121

tests/baselines/reference/logicalAssignment6(target=esnext).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
logicalAssignment6.ts(10,5): error TS2532: Object is possibly 'undefined'.
2-
logicalAssignment6.ts(10,42): error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
2+
logicalAssignment6.ts(10,42): error TS2345: Argument of type '100' is not assignable to parameter of type 'never'.
33

44

55
==== logicalAssignment6.ts (2 errors) ====
@@ -16,6 +16,6 @@ logicalAssignment6.ts(10,42): error TS2345: Argument of type 'number' is not ass
1616
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1717
!!! error TS2532: Object is possibly 'undefined'.
1818
~~~
19-
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
19+
!!! error TS2345: Argument of type '100' is not assignable to parameter of type 'never'.
2020
}
2121

tests/baselines/reference/logicalAssignment7(target=es2015).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
logicalAssignment7.ts(10,5): error TS2532: Object is possibly 'undefined'.
2-
logicalAssignment7.ts(10,40): error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
2+
logicalAssignment7.ts(10,40): error TS2345: Argument of type '100' is not assignable to parameter of type 'never'.
33

44

55
==== logicalAssignment7.ts (2 errors) ====
@@ -16,6 +16,6 @@ logicalAssignment7.ts(10,40): error TS2345: Argument of type 'number' is not ass
1616
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1717
!!! error TS2532: Object is possibly 'undefined'.
1818
~~~
19-
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
19+
!!! error TS2345: Argument of type '100' is not assignable to parameter of type 'never'.
2020
}
2121

tests/baselines/reference/logicalAssignment7(target=es2020).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
logicalAssignment7.ts(10,5): error TS2532: Object is possibly 'undefined'.
2-
logicalAssignment7.ts(10,40): error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
2+
logicalAssignment7.ts(10,40): error TS2345: Argument of type '100' is not assignable to parameter of type 'never'.
33

44

55
==== logicalAssignment7.ts (2 errors) ====
@@ -16,6 +16,6 @@ logicalAssignment7.ts(10,40): error TS2345: Argument of type 'number' is not ass
1616
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1717
!!! error TS2532: Object is possibly 'undefined'.
1818
~~~
19-
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
19+
!!! error TS2345: Argument of type '100' is not assignable to parameter of type 'never'.
2020
}
2121

tests/baselines/reference/logicalAssignment7(target=es2021).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
logicalAssignment7.ts(10,5): error TS2532: Object is possibly 'undefined'.
2-
logicalAssignment7.ts(10,40): error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
2+
logicalAssignment7.ts(10,40): error TS2345: Argument of type '100' is not assignable to parameter of type 'never'.
33

44

55
==== logicalAssignment7.ts (2 errors) ====
@@ -16,6 +16,6 @@ logicalAssignment7.ts(10,40): error TS2345: Argument of type 'number' is not ass
1616
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1717
!!! error TS2532: Object is possibly 'undefined'.
1818
~~~
19-
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
19+
!!! error TS2345: Argument of type '100' is not assignable to parameter of type 'never'.
2020
}
2121

tests/baselines/reference/logicalAssignment7(target=esnext).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
logicalAssignment7.ts(10,5): error TS2532: Object is possibly 'undefined'.
2-
logicalAssignment7.ts(10,40): error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
2+
logicalAssignment7.ts(10,40): error TS2345: Argument of type '100' is not assignable to parameter of type 'never'.
33

44

55
==== logicalAssignment7.ts (2 errors) ====
@@ -16,6 +16,6 @@ logicalAssignment7.ts(10,40): error TS2345: Argument of type 'number' is not ass
1616
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1717
!!! error TS2532: Object is possibly 'undefined'.
1818
~~~
19-
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'never'.
19+
!!! error TS2345: Argument of type '100' is not assignable to parameter of type 'never'.
2020
}
2121

0 commit comments

Comments
 (0)