Skip to content

Commit 8514d0d

Browse files
Merge pull request #7604 from Microsoft/noPrimitiveUnionBreakdown
Avoid elaborating errors when relating primitives to union types of any form.
2 parents 32178ac + e9aeaa2 commit 8514d0d

10 files changed

+2
-36
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5678,7 +5678,7 @@ namespace ts {
56785678
}
56795679
}
56805680
if (target.flags & TypeFlags.Union) {
5681-
if (result = typeRelatedToSomeType(source, <UnionType>target, reportErrors)) {
5681+
if (result = typeRelatedToSomeType(source, <UnionType>target, reportErrors && !(source.flags & TypeFlags.Primitive))) {
56825682
return result;
56835683
}
56845684
}

tests/baselines/reference/contextualTypeWithTuple.errors.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(3,5): error TS232
33
Type '() => number | string | boolean' is not assignable to type '() => number | string'.
44
Type 'number | string | boolean' is not assignable to type 'number | string'.
55
Type 'boolean' is not assignable to type 'number | string'.
6-
Type 'boolean' is not assignable to type 'string'.
76
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(15,1): error TS2322: Type '[number, string, boolean]' is not assignable to type '[number, string]'.
87
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(18,1): error TS2322: Type '[{}, number]' is not assignable to type '[{ a: string; }, number]'.
98
Types of property '0' are incompatible.
@@ -34,7 +33,6 @@ tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(25,1): error TS23
3433
!!! error TS2322: Type '() => number | string | boolean' is not assignable to type '() => number | string'.
3534
!!! error TS2322: Type 'number | string | boolean' is not assignable to type 'number | string'.
3635
!!! error TS2322: Type 'boolean' is not assignable to type 'number | string'.
37-
!!! error TS2322: Type 'boolean' is not assignable to type 'string'.
3836
var numStrBoolTuple: [number, string, boolean] = [5, "foo", true];
3937
var objNumTuple: [{ a: string }, number] = [{ a: "world" }, 5];
4038
var strTupleTuple: [string, [number, {}]] = ["bar", [5, { x: 1, y: 1 }]];

tests/baselines/reference/contextuallyTypedBindingInitializerNegative.errors.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTyp
1414
Types of property '0' are incompatible.
1515
Type 'number' is not assignable to type 'string'.
1616
tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts(26,14): error TS2322: Type '"baz"' is not assignable to type '"foo" | "bar"'.
17-
Type '"baz"' is not assignable to type '"bar"'.
1817

1918

2019
==== tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTypedBindingInitializerNegative.ts (7 errors) ====
@@ -67,5 +66,4 @@ tests/cases/conformance/types/contextualTypes/methodDeclarations/contextuallyTyp
6766
function h({ prop = "baz" }: StringUnion) {}
6867
~~~~
6968
!!! error TS2322: Type '"baz"' is not assignable to type '"foo" | "bar"'.
70-
!!! error TS2322: Type '"baz"' is not assignable to type '"bar"'.
7169

tests/baselines/reference/contextuallyTypedStringLiteralsInJsxAttributes01.errors.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
tests/cases/conformance/types/contextualTypes/jsxAttributes/contextuallyTypedStringLiteralsInJsxAttributes01.tsx(16,15): error TS2322: Type '"f"' is not assignable to type '"A" | "B" | "C"'.
2-
Type '"f"' is not assignable to type '"C"'.
32
tests/cases/conformance/types/contextualTypes/jsxAttributes/contextuallyTypedStringLiteralsInJsxAttributes01.tsx(17,15): error TS2322: Type '"f"' is not assignable to type '"A" | "B" | "C"'.
4-
Type '"f"' is not assignable to type '"C"'.
53

64

75
==== tests/cases/conformance/types/contextualTypes/jsxAttributes/contextuallyTypedStringLiteralsInJsxAttributes01.tsx (2 errors) ====
@@ -23,8 +21,6 @@ tests/cases/conformance/types/contextualTypes/jsxAttributes/contextuallyTypedStr
2321
<FooComponent foo={"f"} />;
2422
~~~~~~~~~
2523
!!! error TS2322: Type '"f"' is not assignable to type '"A" | "B" | "C"'.
26-
!!! error TS2322: Type '"f"' is not assignable to type '"C"'.
2724
<FooComponent foo="f" />;
2825
~~~~~~~
29-
!!! error TS2322: Type '"f"' is not assignable to type '"A" | "B" | "C"'.
30-
!!! error TS2322: Type '"f"' is not assignable to type '"C"'.
26+
!!! error TS2322: Type '"f"' is not assignable to type '"A" | "B" | "C"'.

tests/baselines/reference/destructuringParameterDeclaration2.errors.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(
77
Type '() => number | string[][] | string' is not assignable to type '() => number | string[][]'.
88
Type 'number | string[][] | string' is not assignable to type 'number | string[][]'.
99
Type 'string' is not assignable to type 'number | string[][]'.
10-
Type 'string' is not assignable to type 'string[][]'.
1110
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(16,8): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
1211
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(16,16): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
1312
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(23,14): error TS2345: Argument of type '{ x: string; y: boolean; }' is not assignable to parameter of type '{ x: number; y: any; }'.
@@ -29,7 +28,6 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(
2928
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(38,4): error TS2345: Argument of type '{ b: boolean; }' is not assignable to parameter of type '{ b: number | string; }'.
3029
Types of property 'b' are incompatible.
3130
Type 'boolean' is not assignable to type 'number | string'.
32-
Type 'boolean' is not assignable to type 'string'.
3331
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(39,4): error TS2345: Argument of type '[number, number, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'.
3432
Types of property '2' are incompatible.
3533
Type 'boolean' is not assignable to type '[[any]]'.
@@ -75,7 +73,6 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(
7573
!!! error TS2345: Type '() => number | string[][] | string' is not assignable to type '() => number | string[][]'.
7674
!!! error TS2345: Type 'number | string[][] | string' is not assignable to type 'number | string[][]'.
7775
!!! error TS2345: Type 'string' is not assignable to type 'number | string[][]'.
78-
!!! error TS2345: Type 'string' is not assignable to type 'string[][]'.
7976

8077

8178
// If the declaration includes an initializer expression (which is permitted only
@@ -137,7 +134,6 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(
137134
!!! error TS2345: Argument of type '{ b: boolean; }' is not assignable to parameter of type '{ b: number | string; }'.
138135
!!! error TS2345: Types of property 'b' are incompatible.
139136
!!! error TS2345: Type 'boolean' is not assignable to type 'number | string'.
140-
!!! error TS2345: Type 'boolean' is not assignable to type 'string'.
141137
c5([1, 2, false, true]); // Error, implied type is [any, any, [[any]]]
142138
~~~~~~~~~~~~~~~~~~~
143139
!!! error TS2345: Argument of type '[number, number, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'.

tests/baselines/reference/destructuringParameterDeclaration4.errors.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(11,13): error TS2370: A rest parameter must be of an array type.
22
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(13,13): error TS2370: A rest parameter must be of an array type.
33
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(20,19): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'number | string'.
4-
Type 'boolean' is not assignable to type 'string'.
54
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(21,7): error TS2304: Cannot find name 'array2'.
65
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(22,4): error TS2345: Argument of type '[number, number, string, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'.
76
Types of property '2' are incompatible.
@@ -43,7 +42,6 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(
4342
a1(1, 2, "hello", true); // Error, parameter type is (number|string)[]
4443
~~~~
4544
!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'number | string'.
46-
!!! error TS2345: Type 'boolean' is not assignable to type 'string'.
4745
a1(...array2); // Error parameter type is (number|string)[]
4846
~~~~~~
4947
!!! error TS2304: Cannot find name 'array2'.

tests/baselines/reference/errorMessagesIntersectionTypes02.errors.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
tests/cases/compiler/errorMessagesIntersectionTypes02.ts(14,5): error TS2322: Type '{ fooProp: string; } & Bar' is not assignable to type 'FooBar'.
22
Types of property 'fooProp' are incompatible.
33
Type 'string' is not assignable to type '"hello" | "world"'.
4-
Type 'string' is not assignable to type '"world"'.
54

65

76
==== tests/cases/compiler/errorMessagesIntersectionTypes02.ts (1 errors) ====
@@ -23,6 +22,5 @@ tests/cases/compiler/errorMessagesIntersectionTypes02.ts(14,5): error TS2322: Ty
2322
!!! error TS2322: Type '{ fooProp: string; } & Bar' is not assignable to type 'FooBar'.
2423
!!! error TS2322: Types of property 'fooProp' are incompatible.
2524
!!! error TS2322: Type 'string' is not assignable to type '"hello" | "world"'.
26-
!!! error TS2322: Type 'string' is not assignable to type '"world"'.
2725
fooProp: "frizzlebizzle"
2826
});

tests/baselines/reference/genericCallWithTupleType.errors.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithTup
33
Type '() => string | number | boolean' is not assignable to type '() => string | number'.
44
Type 'string | number | boolean' is not assignable to type 'string | number'.
55
Type 'boolean' is not assignable to type 'string | number'.
6-
Type 'boolean' is not assignable to type 'number'.
76
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithTupleType.ts(14,1): error TS2322: Type '{ a: string; }' is not assignable to type 'string | number'.
87
Type '{ a: string; }' is not assignable to type 'number'.
98
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithTupleType.ts(22,1): error TS2322: Type '[number, string]' is not assignable to type '[string, number]'.
@@ -35,7 +34,6 @@ tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithTup
3534
!!! error TS2322: Type '() => string | number | boolean' is not assignable to type '() => string | number'.
3635
!!! error TS2322: Type 'string | number | boolean' is not assignable to type 'string | number'.
3736
!!! error TS2322: Type 'boolean' is not assignable to type 'string | number'.
38-
!!! error TS2322: Type 'boolean' is not assignable to type 'number'.
3937
var e3 = i1.tuple1[2]; // {}
4038
i1.tuple1[3] = { a: "string" };
4139
~~~~~~~~~~~~

tests/baselines/reference/iteratorSpreadInCall6.errors.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
tests/cases/conformance/es6/spread/iteratorSpreadInCall6.ts(1,28): error TS2345: Argument of type 'string' is not assignable to parameter of type 'symbol | number'.
2-
Type 'string' is not assignable to type 'number'.
32

43

54
==== tests/cases/conformance/es6/spread/iteratorSpreadInCall6.ts (1 errors) ====
65
foo(...new SymbolIterator, ...new StringIterator);
76
~~~~~~~~~~~~~~~~~~~~~
87
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'symbol | number'.
9-
!!! error TS2345: Type 'string' is not assignable to type 'number'.
108

119
function foo(...s: (symbol | number)[]) { }
1210
class SymbolIterator {

tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0
44
tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(40,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'.
55
tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(41,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'.
66
tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(44,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'.
7-
Type 'string' is not assignable to type '"World"'.
87
tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(45,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'.
9-
Type 'string' is not assignable to type '"World"'.
108
tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(46,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'.
11-
Type 'string' is not assignable to type '"World"'.
129
tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(47,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'.
13-
Type 'string' is not assignable to type '"World"'.
1410
tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(48,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'.
15-
Type 'string' is not assignable to type '"World"'.
1611
tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(55,43): error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'.
1712
tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(57,52): error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'.
1813
tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(58,43): error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'.
@@ -26,9 +21,7 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0
2621
tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(88,43): error TS2345: Argument of type '"Hello"' is not assignable to parameter of type '"World"'.
2722
tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(89,52): error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'.
2823
tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(93,5): error TS2322: Type 'string' is not assignable to type '"Hello" | "World"'.
29-
Type 'string' is not assignable to type '"World"'.
3024
tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(97,5): error TS2322: Type 'string' is not assignable to type '"Hello" | "World"'.
31-
Type 'string' is not assignable to type '"World"'.
3225
tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(100,25): error TS2345: Argument of type '"Hello" | "World"' is not assignable to parameter of type '"Hello"'.
3326
Type '"World"' is not assignable to type '"Hello"'.
3427
tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(104,25): error TS2345: Argument of type '"Hello" | "World"' is not assignable to parameter of type '"Hello"'.
@@ -92,23 +85,18 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0
9285
a = takeReturnHelloWorld(a);
9386
~
9487
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'.
95-
!!! error TS2345: Type 'string' is not assignable to type '"World"'.
9688
b = takeReturnHelloWorld(b);
9789
~
9890
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'.
99-
!!! error TS2345: Type 'string' is not assignable to type '"World"'.
10091
c = takeReturnHelloWorld(c);
10192
~
10293
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'.
103-
!!! error TS2345: Type 'string' is not assignable to type '"World"'.
10494
d = takeReturnHelloWorld(d);
10595
~
10696
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'.
107-
!!! error TS2345: Type 'string' is not assignable to type '"World"'.
10897
e = takeReturnHelloWorld(e);
10998
~
11099
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'.
111-
!!! error TS2345: Type 'string' is not assignable to type '"World"'.
112100
}
113101

114102
namespace n2 {
@@ -178,14 +166,12 @@ tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes0
178166
a = takeReturnString(a);
179167
~
180168
!!! error TS2322: Type 'string' is not assignable to type '"Hello" | "World"'.
181-
!!! error TS2322: Type 'string' is not assignable to type '"World"'.
182169
b = takeReturnString(b);
183170
c = takeReturnString(c);
184171
d = takeReturnString(d);
185172
e = takeReturnString(e);
186173
~
187174
!!! error TS2322: Type 'string' is not assignable to type '"Hello" | "World"'.
188-
!!! error TS2322: Type 'string' is not assignable to type '"World"'.
189175

190176
// Passing these as arguments should cause an error.
191177
a = takeReturnHello(a);

0 commit comments

Comments
 (0)