Skip to content

Commit 84f5aa5

Browse files
authored
Put error spans deep on nested object literals (microsoft#25140)
* Add ncie deep elaborations * Nice stuff * Modify tuple error to use length error mroe often * Accept good baselines * Accept meh baselines * Fix literal types * Calculate elaborations like it was the very first time again~ * Use tristate for enum relationship to ensure elaborations are printed at least once * Update message text, nits * move some functions back to where they were * Add test of deep JSX elaboration * Add elaboration test with parenthesized expressions, comma expressions, and assignments * Move check to allow elaborations on more anonymous types * Fix nits * Add specialized error to elaborations of nonliteral computed named-members * Update error message
1 parent e4145e3 commit 84f5aa5

File tree

85 files changed

+1388
-706
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+1388
-706
lines changed

src/compiler/checker.ts

+189-49
Large diffs are not rendered by default.

src/compiler/diagnosticMessages.json

+13
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,10 @@
14521452
"category": "Error",
14531453
"code": 2417
14541454
},
1455+
"Type of computed property's value is '{0}', which is not assignable to type '{1}'.": {
1456+
"category": "Error",
1457+
"code": 2418
1458+
},
14551459
"Class '{0}' incorrectly implements interface '{1}'.": {
14561460
"category": "Error",
14571461
"code": 2420
@@ -3754,6 +3758,15 @@
37543758
"code": 6371
37553759
},
37563760

3761+
"The expected type comes from property '{0}' which is declared here on type '{1}'": {
3762+
"category": "Message",
3763+
"code": 6500
3764+
},
3765+
"The expected type comes from this index signature.": {
3766+
"category": "Message",
3767+
"code": 6501
3768+
},
3769+
37573770
"Variable '{0}' implicitly has an '{1}' type.": {
37583771
"category": "Error",
37593772
"code": 7005

src/tsconfig-base.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"pretty": true,
4-
"lib": ["es5"],
4+
"lib": ["es2015"],
55
"target": "es5",
66

77
"declaration": true,

tests/baselines/reference/api/tsserverlibrary.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -5193,6 +5193,7 @@ declare namespace ts {
51935193
Class_0_incorrectly_extends_base_class_1: DiagnosticMessage;
51945194
Property_0_in_type_1_is_not_assignable_to_the_same_property_in_base_type_2: DiagnosticMessage;
51955195
Class_static_side_0_incorrectly_extends_base_class_static_side_1: DiagnosticMessage;
5196+
Type_of_computed_property_s_value_is_0_which_is_not_assignable_to_type_1: DiagnosticMessage;
51965197
Class_0_incorrectly_implements_interface_1: DiagnosticMessage;
51975198
A_class_may_only_implement_another_class_or_interface: DiagnosticMessage;
51985199
Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_accessor: DiagnosticMessage;
@@ -5765,6 +5766,8 @@ declare namespace ts {
57655766
Option_build_must_be_the_first_command_line_argument: DiagnosticMessage;
57665767
Options_0_and_1_cannot_be_combined: DiagnosticMessage;
57675768
Skipping_clean_because_not_all_projects_could_be_located: DiagnosticMessage;
5769+
The_expected_type_comes_from_property_0_which_is_declared_here_on_type_1: DiagnosticMessage;
5770+
The_expected_type_comes_from_this_index_signature: DiagnosticMessage;
57685771
Variable_0_implicitly_has_an_1_type: DiagnosticMessage;
57695772
Parameter_0_implicitly_has_an_1_type: DiagnosticMessage;
57705773
Member_0_implicitly_has_an_1_type: DiagnosticMessage;

tests/baselines/reference/arrayLiterals3.errors.txt

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(10,5): error TS2322: Type '[]' is not assignable to type '[any, any, any]'.
22
Property '0' is missing in type '[]'.
3-
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,5): error TS2322: Type '[string, number, boolean]' is not assignable to type '[boolean, string, number]'.
4-
Type 'string' is not assignable to type 'boolean'.
3+
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,38): error TS2322: Type 'string' is not assignable to type 'boolean'.
4+
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,48): error TS2322: Type 'number' is not assignable to type 'string'.
5+
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,51): error TS2322: Type 'true' is not assignable to type 'number'.
56
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(17,5): error TS2322: Type '[number, number, string, boolean]' is not assignable to type '[number, number]'.
67
Types of property 'length' are incompatible.
78
Type '4' is not assignable to type '2'.
@@ -18,7 +19,7 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error
1819
'number' is a primitive, but 'Number' is a wrapper object. Prefer using 'number' when possible.
1920

2021

21-
==== tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts (6 errors) ====
22+
==== tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts (8 errors) ====
2223
// Each element expression in a non-empty array literal is processed as follows:
2324
// - If the array literal contains no spread elements, and if the array literal is contextually typed (section 4.19)
2425
// by a type T and T has a property with the numeric name N, where N is the index of the element expression in the array literal,
@@ -33,9 +34,12 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error
3334
!!! error TS2322: Type '[]' is not assignable to type '[any, any, any]'.
3435
!!! error TS2322: Property '0' is missing in type '[]'.
3536
var a1: [boolean, string, number] = ["string", 1, true]; // Error
36-
~~
37-
!!! error TS2322: Type '[string, number, boolean]' is not assignable to type '[boolean, string, number]'.
38-
!!! error TS2322: Type 'string' is not assignable to type 'boolean'.
37+
~~~~~~~~
38+
!!! error TS2322: Type 'string' is not assignable to type 'boolean'.
39+
~
40+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
41+
~~~~
42+
!!! error TS2322: Type 'true' is not assignable to type 'number'.
3943

4044
// The resulting type an array literal expression is determined as follows:
4145
// - If the array literal contains no spread elements and is an array assignment pattern in a destructuring assignment (section 4.17.1),

tests/baselines/reference/assignmentToObjectAndFunction.errors.txt

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
tests/cases/compiler/assignmentToObjectAndFunction.ts(1,5): error TS2322: Type '{ toString: number; }' is not assignable to type 'Object'.
2-
Types of property 'toString' are incompatible.
3-
Type 'number' is not assignable to type '() => string'.
1+
tests/cases/compiler/assignmentToObjectAndFunction.ts(1,24): error TS2322: Type 'number' is not assignable to type '() => string'.
42
tests/cases/compiler/assignmentToObjectAndFunction.ts(8,5): error TS2322: Type '{}' is not assignable to type 'Function'.
53
Property 'apply' is missing in type '{}'.
64
tests/cases/compiler/assignmentToObjectAndFunction.ts(29,5): error TS2322: Type 'typeof bad' is not assignable to type 'Function'.
@@ -10,10 +8,9 @@ tests/cases/compiler/assignmentToObjectAndFunction.ts(29,5): error TS2322: Type
108

119
==== tests/cases/compiler/assignmentToObjectAndFunction.ts (3 errors) ====
1210
var errObj: Object = { toString: 0 }; // Error, incompatible toString
13-
~~~~~~
14-
!!! error TS2322: Type '{ toString: number; }' is not assignable to type 'Object'.
15-
!!! error TS2322: Types of property 'toString' are incompatible.
16-
!!! error TS2322: Type 'number' is not assignable to type '() => string'.
11+
~~~~~~~~
12+
!!! error TS2322: Type 'number' is not assignable to type '() => string'.
13+
!!! related TS6500 /.ts/lib.es5.d.ts:125:5: The expected type comes from property 'toString' which is declared here on type 'Object'
1714
var goodObj: Object = {
1815
toString(x?) {
1916
return "";

tests/baselines/reference/assignmentToParenthesizedIdentifiers.errors.txt

+12-21
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@ tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesize
66
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(17,1): error TS2539: Cannot assign to 'M' because it is not a variable.
77
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(18,2): error TS2539: Cannot assign to 'M' because it is not a variable.
88
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(25,5): error TS2539: Cannot assign to 'M3' because it is not a variable.
9-
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(31,1): error TS2322: Type '{ x: string; }' is not assignable to type 'typeof M3'.
10-
Types of property 'x' are incompatible.
11-
Type 'string' is not assignable to type 'number'.
12-
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(32,1): error TS2322: Type '{ x: string; }' is not assignable to type 'typeof M3'.
13-
Types of property 'x' are incompatible.
14-
Type 'string' is not assignable to type 'number'.
15-
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(33,1): error TS2322: Type '{ x: string; }' is not assignable to type 'typeof M3'.
16-
Types of property 'x' are incompatible.
17-
Type 'string' is not assignable to type 'number'.
9+
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(31,11): error TS2322: Type 'string' is not assignable to type 'number'.
10+
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(32,13): error TS2322: Type 'string' is not assignable to type 'number'.
11+
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(33,13): error TS2322: Type 'string' is not assignable to type 'number'.
1812
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(37,1): error TS2539: Cannot assign to 'fn' because it is not a variable.
1913
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(38,2): error TS2539: Cannot assign to 'fn' because it is not a variable.
2014
tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts(43,5): error TS2322: Type '""' is not assignable to type 'number'.
@@ -78,20 +72,17 @@ tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesize
7872
(M2.M3) = { x: 3 }; // OK
7973

8074
M2.M3 = { x: '' }; // Error
81-
~~~~~
82-
!!! error TS2322: Type '{ x: string; }' is not assignable to type 'typeof M3'.
83-
!!! error TS2322: Types of property 'x' are incompatible.
84-
!!! error TS2322: Type 'string' is not assignable to type 'number'.
75+
~
76+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
77+
!!! related TS6500 tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts:22:20: The expected type comes from property 'x' which is declared here on type 'typeof M3'
8578
(M2).M3 = { x: '' }; // Error
86-
~~~~~~~
87-
!!! error TS2322: Type '{ x: string; }' is not assignable to type 'typeof M3'.
88-
!!! error TS2322: Types of property 'x' are incompatible.
89-
!!! error TS2322: Type 'string' is not assignable to type 'number'.
79+
~
80+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
81+
!!! related TS6500 tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts:22:20: The expected type comes from property 'x' which is declared here on type 'typeof M3'
9082
(M2.M3) = { x: '' }; // Error
91-
~~~~~~~
92-
!!! error TS2322: Type '{ x: string; }' is not assignable to type 'typeof M3'.
93-
!!! error TS2322: Types of property 'x' are incompatible.
94-
!!! error TS2322: Type 'string' is not assignable to type 'number'.
83+
~
84+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
85+
!!! related TS6500 tests/cases/conformance/expressions/valuesAndReferences/assignmentToParenthesizedIdentifiers.ts:22:20: The expected type comes from property 'x' which is declared here on type 'typeof M3'
9586

9687

9788
function fn() { }

tests/baselines/reference/baseClassImprovedMismatchErrors.errors.txt

-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ tests/cases/compiler/baseClassImprovedMismatchErrors.ts(8,5): error TS2416: Prop
55
Types of property 'n' are incompatible.
66
Type 'string | Derived' is not assignable to type 'string | Base'.
77
Type 'Derived' is not assignable to type 'string | Base'.
8-
Type 'Derived' is not assignable to type 'Base'.
98
tests/cases/compiler/baseClassImprovedMismatchErrors.ts(9,5): error TS2416: Property 'fn' in type 'Derived' is not assignable to the same property in base type 'Base'.
109
Type '() => string | number' is not assignable to type '() => number'.
1110
Type 'string | number' is not assignable to type 'number'.
@@ -17,7 +16,6 @@ tests/cases/compiler/baseClassImprovedMismatchErrors.ts(14,5): error TS2416: Pro
1716
Types of property 'n' are incompatible.
1817
Type 'string | DerivedInterface' is not assignable to type 'string | Base'.
1918
Type 'DerivedInterface' is not assignable to type 'string | Base'.
20-
Type 'DerivedInterface' is not assignable to type 'Base'.
2119
tests/cases/compiler/baseClassImprovedMismatchErrors.ts(15,5): error TS2416: Property 'fn' in type 'DerivedInterface' is not assignable to the same property in base type 'Base'.
2220
Type '() => string | number' is not assignable to type '() => number'.
2321
Type 'string | number' is not assignable to type 'number'.
@@ -41,7 +39,6 @@ tests/cases/compiler/baseClassImprovedMismatchErrors.ts(15,5): error TS2416: Pro
4139
!!! error TS2416: Types of property 'n' are incompatible.
4240
!!! error TS2416: Type 'string | Derived' is not assignable to type 'string | Base'.
4341
!!! error TS2416: Type 'Derived' is not assignable to type 'string | Base'.
44-
!!! error TS2416: Type 'Derived' is not assignable to type 'Base'.
4542
fn() {
4643
~~
4744
!!! error TS2416: Property 'fn' in type 'Derived' is not assignable to the same property in base type 'Base'.
@@ -61,7 +58,6 @@ tests/cases/compiler/baseClassImprovedMismatchErrors.ts(15,5): error TS2416: Pro
6158
!!! error TS2416: Types of property 'n' are incompatible.
6259
!!! error TS2416: Type 'string | DerivedInterface' is not assignable to type 'string | Base'.
6360
!!! error TS2416: Type 'DerivedInterface' is not assignable to type 'string | Base'.
64-
!!! error TS2416: Type 'DerivedInterface' is not assignable to type 'Base'.
6561
fn() {
6662
~~
6763
!!! error TS2416: Property 'fn' in type 'DerivedInterface' is not assignable to the same property in base type 'Base'.

tests/baselines/reference/checkJsxGenericTagHasCorrectInferences.errors.txt

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
tests/cases/conformance/jsx/file.tsx(13,54): error TS2326: Types of property 'nextValues' are incompatible.
2-
Type '(a: { x: string; }) => string' is not assignable to type '(cur: { x: string; }) => { x: string; }'.
3-
Type 'string' is not assignable to type '{ x: string; }'.
1+
tests/cases/conformance/jsx/file.tsx(13,54): error TS2322: Type '(a: { x: string; }) => string' is not assignable to type '(cur: { x: string; }) => { x: string; }'.
2+
Type 'string' is not assignable to type '{ x: string; }'.
43

54

65
==== tests/cases/conformance/jsx/file.tsx (1 errors) ====
@@ -17,7 +16,7 @@ tests/cases/conformance/jsx/file.tsx(13,54): error TS2326: Types of property 'ne
1716
let b = <GenericComponent initialValues={12} nextValues={a => a} />; // No error - Values should be reinstantiated with `number` (since `object` is a default, not a constraint)
1817
let c = <GenericComponent initialValues={{ x: "y" }} nextValues={a => ({ x: a.x })} />; // No Error
1918
let d = <GenericComponent initialValues={{ x: "y" }} nextValues={a => a.x} />; // Error - `string` is not assignable to `{x: string}`
20-
~~~~~~~~~~~~~~~~~~~~~
21-
!!! error TS2326: Types of property 'nextValues' are incompatible.
22-
!!! error TS2326: Type '(a: { x: string; }) => string' is not assignable to type '(cur: { x: string; }) => { x: string; }'.
23-
!!! error TS2326: Type 'string' is not assignable to type '{ x: string; }'.
19+
~~~~~~~~~~
20+
!!! error TS2322: Type '(a: { x: string; }) => string' is not assignable to type '(cur: { x: string; }) => { x: string; }'.
21+
!!! error TS2322: Type 'string' is not assignable to type '{ x: string; }'.
22+
!!! related TS6500 tests/cases/conformance/jsx/file.tsx:13:54: The expected type comes from property 'nextValues' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<GenericComponent<{ initialValues: { x: string; }; nextValues: {}; }, { x: string; }>> & { initialValues: { x: string; }; nextValues: {}; } & BaseProps<{ x: string; }> & { children?: ReactNode; }'
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts(6,5): error TS2322: Type '{ [x: string]: string | number; }' is not assignable to type 'I'.
2-
Index signatures are incompatible.
3-
Type 'string | number' is not assignable to type 'boolean'.
4-
Type 'string' is not assignable to type 'boolean'.
1+
tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts(7,5): error TS2418: Type of computed property's value is 'string', which is not assignable to type 'boolean'.
2+
tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts(8,5): error TS2418: Type of computed property's value is 'number', which is not assignable to type 'boolean'.
53

64

7-
==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts (1 errors) ====
5+
==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts (2 errors) ====
86
interface I {
97
[s: string]: boolean;
108
[s: number]: boolean;
119
}
1210

1311
var o: I = {
14-
~
15-
!!! error TS2322: Type '{ [x: string]: string | number; }' is not assignable to type 'I'.
16-
!!! error TS2322: Index signatures are incompatible.
17-
!!! error TS2322: Type 'string | number' is not assignable to type 'boolean'.
18-
!!! error TS2322: Type 'string' is not assignable to type 'boolean'.
1912
[""+"foo"]: "",
13+
~~~~~~~~~~
14+
!!! error TS2418: Type of computed property's value is 'string', which is not assignable to type 'boolean'.
15+
!!! related TS6501 tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts:2:5: The expected type comes from this index signature.
2016
[""+"bar"]: 0
17+
~~~~~~~~~~
18+
!!! error TS2418: Type of computed property's value is 'number', which is not assignable to type 'boolean'.
19+
!!! related TS6501 tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts:2:5: The expected type comes from this index signature.
2120
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES6.ts(6,5): error TS2322: Type '{ [x: string]: string | number; }' is not assignable to type 'I'.
2-
Index signatures are incompatible.
3-
Type 'string | number' is not assignable to type 'boolean'.
4-
Type 'string' is not assignable to type 'boolean'.
1+
tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES6.ts(7,5): error TS2418: Type of computed property's value is 'string', which is not assignable to type 'boolean'.
2+
tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES6.ts(8,5): error TS2418: Type of computed property's value is 'number', which is not assignable to type 'boolean'.
53

64

7-
==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES6.ts (1 errors) ====
5+
==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES6.ts (2 errors) ====
86
interface I {
97
[s: string]: boolean;
108
[s: number]: boolean;
119
}
1210

1311
var o: I = {
14-
~
15-
!!! error TS2322: Type '{ [x: string]: string | number; }' is not assignable to type 'I'.
16-
!!! error TS2322: Index signatures are incompatible.
17-
!!! error TS2322: Type 'string | number' is not assignable to type 'boolean'.
18-
!!! error TS2322: Type 'string' is not assignable to type 'boolean'.
1912
[""+"foo"]: "",
13+
~~~~~~~~~~
14+
!!! error TS2418: Type of computed property's value is 'string', which is not assignable to type 'boolean'.
15+
!!! related TS6501 tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES6.ts:2:5: The expected type comes from this index signature.
2016
[""+"bar"]: 0
17+
~~~~~~~~~~
18+
!!! error TS2418: Type of computed property's value is 'number', which is not assignable to type 'boolean'.
19+
!!! related TS6501 tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES6.ts:2:5: The expected type comes from this index signature.
2120
}

0 commit comments

Comments
 (0)