Skip to content

Commit c8f6392

Browse files
committed
Stop looking at binding patterns for type argument inference entirely
1 parent 663fcdd commit c8f6392

9 files changed

+68
-30
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25274,7 +25274,7 @@ namespace ts {
2527425274
if (result) {
2527525275
return result;
2527625276
}
25277-
if (!(contextFlags! & ContextFlags.SkipBindingPatterns) && isBindingPattern(declaration.name) && declaration.name.elements.length > 0) {
25277+
if (!(contextFlags! & ContextFlags.SkipBindingPatterns) && isBindingPattern(declaration.name)) {
2527825278
// This is less a contextual type and more an implied shape - in some cases, this may be undesirable
2527925279
return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false);
2528025280
}
@@ -28574,7 +28574,7 @@ namespace ts {
2857428574
// 'let f: (x: string) => number = wrap(s => s.length)', we infer from the declared type of 'f' to the
2857528575
// return type of 'wrap'.
2857628576
if (node.kind !== SyntaxKind.Decorator) {
28577-
const contextualType = getContextualType(node, every(signature.typeParameters, p => !!getDefaultFromTypeParameter(p)) ? ContextFlags.SkipBindingPatterns : ContextFlags.None);
28577+
const contextualType = getContextualType(node, ContextFlags.SkipBindingPatterns);
2857828578
if (contextualType) {
2857928579
// We clone the inference context to avoid disturbing a resolution in progress for an
2858028580
// outer call expression. Effectively we just want a snapshot of whatever has been
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/declarationEmitDestructuring4.ts(9,22): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
2+
3+
4+
==== tests/cases/compiler/declarationEmitDestructuring4.ts (1 errors) ====
5+
// For an array binding pattern with empty elements,
6+
// we will not make any modification and will emit
7+
// the similar binding pattern users' have written
8+
function baz([]) { }
9+
function baz1([] = [1,2,3]) { }
10+
function baz2([[]] = [[1,2,3]]) { }
11+
12+
function baz3({}) { }
13+
function baz4({} = { x: 10 }) { }
14+
~
15+
!!! error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
16+
17+

tests/baselines/reference/declarationEmitDestructuringObjectLiteralPattern.errors.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(1,13): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
2+
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(1,19): error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
13
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(2,23): error TS2353: Object literal may only specify known properties, and 'y4' does not exist in type '{ x4: any; }'.
24
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(3,16): error TS2353: Object literal may only specify known properties, and 'x5' does not exist in type '{ y5: any; }'.
35
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(5,27): error TS2353: Object literal may only specify known properties, and 'y7' does not exist in type '{ x7: any; }'.
46
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(6,20): error TS2353: Object literal may only specify known properties, and 'x8' does not exist in type '{ y8: any; }'.
57

68

7-
==== tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts (4 errors) ====
9+
==== tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts (6 errors) ====
810
var { } = { x: 5, y: "hello" };
11+
~
12+
!!! error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
13+
~
14+
!!! error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
915
var { x4 } = { x4: 5, y4: "hello" };
1016
~~
1117
!!! error TS2353: Object literal may only specify known properties, and 'y4' does not exist in type '{ x4: any; }'.

tests/baselines/reference/declarationEmitDestructuringObjectLiteralPattern1.errors.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern1.ts(1,13): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
2+
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern1.ts(1,19): error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
13
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern1.ts(2,23): error TS2353: Object literal may only specify known properties, and 'y4' does not exist in type '{ x4: any; }'.
24
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern1.ts(3,16): error TS2353: Object literal may only specify known properties, and 'x5' does not exist in type '{ y5: any; }'.
35
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern1.ts(5,27): error TS2353: Object literal may only specify known properties, and 'y7' does not exist in type '{ x7: any; }'.
46
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern1.ts(6,20): error TS2353: Object literal may only specify known properties, and 'x8' does not exist in type '{ y8: any; }'.
57

68

7-
==== tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern1.ts (4 errors) ====
9+
==== tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern1.ts (6 errors) ====
810
var { } = { x: 5, y: "hello" };
11+
~
12+
!!! error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
13+
~
14+
!!! error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
915
var { x4 } = { x4: 5, y4: "hello" };
1016
~~
1117
!!! error TS2353: Object literal may only specify known properties, and 'y4' does not exist in type '{ x4: any; }'.

tests/baselines/reference/declarationsAndAssignments.errors.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(5,16): error TS2493: Tuple type '[number, string]' of length '2' has no element at index '2'.
2+
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(22,17): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
3+
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(22,23): error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
24
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(23,25): error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{ x: any; }'.
35
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(24,19): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{ y: any; }'.
46
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(28,28): error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{ x: any; }'.
@@ -20,7 +22,7 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,6):
2022
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9): error TS2322: Type 'number' is not assignable to type 'string'.
2123

2224

23-
==== tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts (20 errors) ====
25+
==== tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts (22 errors) ====
2426
function f0() {
2527
var [] = [1, "hello"];
2628
var [x] = [1, "hello"];
@@ -45,6 +47,10 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9):
4547

4648
function f2() {
4749
var { } = { x: 5, y: "hello" }; // Error, no x and y in target
50+
~
51+
!!! error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
52+
~
53+
!!! error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
4854
var { x } = { x: 5, y: "hello" }; // Error, no y in target
4955
~
5056
!!! error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{ x: any; }'.

tests/baselines/reference/destructuringTuple.errors.txt

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
tests/cases/compiler/destructuringTuple.ts(11,7): error TS2461: Type 'number' is not an array type.
2-
tests/cases/compiler/destructuringTuple.ts(11,48): error TS2769: No overload matches this call.
3-
Overload 1 of 3, '(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number', gave the following error.
4-
Type 'never[]' is not assignable to type 'number'.
5-
Overload 2 of 3, '(callbackfn: (previousValue: [], currentValue: number, currentIndex: number, array: number[]) => [], initialValue: []): []', gave the following error.
6-
Type 'never[]' is not assignable to type '[]'.
7-
Target allows only 0 element(s) but source may have more.
81
tests/cases/compiler/destructuringTuple.ts(11,60): error TS2769: No overload matches this call.
92
Overload 1 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
103
Argument of type 'number' is not assignable to parameter of type 'ConcatArray<never>'.
114
Overload 2 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
125
Argument of type 'number' is not assignable to parameter of type 'ConcatArray<never>'.
136

147

15-
==== tests/cases/compiler/destructuringTuple.ts (3 errors) ====
8+
==== tests/cases/compiler/destructuringTuple.ts (1 errors) ====
169
declare var tuple: [boolean, number, ...string[]];
1710

1811
const [a, b, c, ...rest] = tuple;
@@ -24,17 +17,6 @@ tests/cases/compiler/destructuringTuple.ts(11,60): error TS2769: No overload mat
2417
// Repros from #32140
2518

2619
const [oops1] = [1, 2, 3].reduce((accu, el) => accu.concat(el), []);
27-
~~~~~~~
28-
!!! error TS2461: Type 'number' is not an array type.
29-
~~~~~~~~~~~~~~~
30-
!!! error TS2769: No overload matches this call.
31-
!!! error TS2769: Overload 1 of 3, '(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number', gave the following error.
32-
!!! error TS2769: Type 'never[]' is not assignable to type 'number'.
33-
!!! error TS2769: Overload 2 of 3, '(callbackfn: (previousValue: [], currentValue: number, currentIndex: number, array: number[]) => [], initialValue: []): []', gave the following error.
34-
!!! error TS2769: Type 'never[]' is not assignable to type '[]'.
35-
!!! error TS2769: Target allows only 0 element(s) but source may have more.
36-
!!! related TS6502 /.ts/lib.es5.d.ts:1412:24: The expected type comes from the return type of this signature.
37-
!!! related TS6502 /.ts/lib.es5.d.ts:1418:27: The expected type comes from the return type of this signature.
3820
~~
3921
!!! error TS2769: No overload matches this call.
4022
!!! error TS2769: Overload 1 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.

tests/baselines/reference/destructuringTuple.types

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ declare var receiver: typeof tuple;
2323
// Repros from #32140
2424

2525
const [oops1] = [1, 2, 3].reduce((accu, el) => accu.concat(el), []);
26-
>oops1 : any
27-
>[1, 2, 3].reduce((accu, el) => accu.concat(el), []) : number
26+
>oops1 : never
27+
>[1, 2, 3].reduce((accu, el) => accu.concat(el), []) : never[]
2828
>[1, 2, 3].reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; <U>(callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; }
2929
>[1, 2, 3] : number[]
3030
>1 : 1
3131
>2 : 2
3232
>3 : 3
3333
>reduce : { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; <U>(callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; }
34-
>(accu, el) => accu.concat(el) : (accu: [], el: number) => never[]
35-
>accu : []
34+
>(accu, el) => accu.concat(el) : (accu: never[], el: number) => never[]
35+
>accu : never[]
3636
>el : number
3737
>accu.concat(el) : never[]
3838
>accu.concat : { (...items: ConcatArray<never>[]): never[]; (...items: ConcatArray<never>[]): never[]; }
39-
>accu : []
39+
>accu : never[]
4040
>concat : { (...items: ConcatArray<never>[]): never[]; (...items: ConcatArray<never>[]): never[]; }
4141
>el : number
4242
>[] : never[]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tests/cases/conformance/es6/destructuring/emptyObjectBindingPatternParameter04.ts(1,18): error TS2353: Object literal may only specify known properties, and 'a' does not exist in type '{}'.
2+
tests/cases/conformance/es6/destructuring/emptyObjectBindingPatternParameter04.ts(1,24): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type '{}'.
3+
tests/cases/conformance/es6/destructuring/emptyObjectBindingPatternParameter04.ts(1,32): error TS2353: Object literal may only specify known properties, and 'c' does not exist in type '{}'.
4+
5+
6+
==== tests/cases/conformance/es6/destructuring/emptyObjectBindingPatternParameter04.ts (3 errors) ====
7+
function f({} = {a: 1, b: "2", c: true}) {
8+
~
9+
!!! error TS2353: Object literal may only specify known properties, and 'a' does not exist in type '{}'.
10+
~
11+
!!! error TS2353: Object literal may only specify known properties, and 'b' does not exist in type '{}'.
12+
~
13+
!!! error TS2353: Object literal may only specify known properties, and 'c' does not exist in type '{}'.
14+
var x, y, z;
15+
}

tests/baselines/reference/missingAndExcessProperties.errors.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts(12,8): e
1010
tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts(12,11): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
1111
tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts(13,18): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
1212
tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts(14,8): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
13+
tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts(20,17): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
14+
tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts(20,23): error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
1315
tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts(21,25): error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{ x: any; }'.
1416
tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts(22,19): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{ y: any; }'.
1517
tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts(29,14): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
@@ -18,7 +20,7 @@ tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts(30,22):
1820
tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts(31,16): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{ y: number; }'.
1921

2022

21-
==== tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts (18 errors) ====
23+
==== tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts (20 errors) ====
2224
// Missing properties
2325
function f1() {
2426
var { x, y } = {};
@@ -67,6 +69,10 @@ tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts(31,16):
6769
// Excess properties
6870
function f3() {
6971
var { } = { x: 0, y: 0 };
72+
~
73+
!!! error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
74+
~
75+
!!! error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
7076
var { x } = { x: 0, y: 0 };
7177
~
7278
!!! error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{ x: any; }'.

0 commit comments

Comments
 (0)