Skip to content

Commit 663fcdd

Browse files
committed
Ignore empty binding patterns for contextual typing
1 parent ba3a068 commit 663fcdd

8 files changed

+15
-61
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25274,7 +25274,8 @@ namespace ts {
2527425274
if (result) {
2527525275
return result;
2527625276
}
25277-
if (!(contextFlags! & ContextFlags.SkipBindingPatterns) && isBindingPattern(declaration.name)) { // This is less a contextual type and more an implied shape - in some cases, this may be undesirable
25277+
if (!(contextFlags! & ContextFlags.SkipBindingPatterns) && isBindingPattern(declaration.name) && declaration.name.elements.length > 0) {
25278+
// This is less a contextual type and more an implied shape - in some cases, this may be undesirable
2527825279
return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false);
2527925280
}
2528025281
}

tests/baselines/reference/declarationEmitDestructuring4.errors.txt

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/baselines/reference/declarationEmitDestructuringObjectLiteralPattern.errors.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
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 '{}'.
31
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; }'.
42
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; }'.
53
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; }'.
64
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; }'.
75

86

9-
==== tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts (6 errors) ====
7+
==== tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts (4 errors) ====
108
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 '{}'.
159
var { x4 } = { x4: 5, y4: "hello" };
1610
~~
1711
!!! 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: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
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 '{}'.
31
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; }'.
42
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; }'.
53
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; }'.
64
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; }'.
75

86

9-
==== tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern1.ts (6 errors) ====
7+
==== tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern1.ts (4 errors) ====
108
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 '{}'.
159
var { x4 } = { x4: 5, y4: "hello" };
1610
~~
1711
!!! 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: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
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 '{}'.
42
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; }'.
53
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; }'.
64
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; }'.
@@ -22,7 +20,7 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,6):
2220
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9): error TS2322: Type 'number' is not assignable to type 'string'.
2321

2422

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

4846
function f2() {
4947
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 '{}'.
5448
var { x } = { x: 5, y: "hello" }; // Error, no y in target
5549
~
5650
!!! error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{ x: any; }'.

tests/baselines/reference/emptyObjectBindingPatternParameter04.errors.txt

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/baselines/reference/missingAndExcessProperties.errors.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ 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 '{}'.
1513
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; }'.
1614
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; }'.
1715
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 '{}'.
@@ -20,7 +18,7 @@ tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts(30,22):
2018
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; }'.
2119

2220

23-
==== tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts (20 errors) ====
21+
==== tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts (18 errors) ====
2422
// Missing properties
2523
function f1() {
2624
var { x, y } = {};
@@ -69,10 +67,6 @@ tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts(31,16):
6967
// Excess properties
7068
function f3() {
7169
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 '{}'.
7670
var { x } = { x: 0, y: 0 };
7771
~
7872
!!! error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{ x: any; }'.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// declare function pick<T, K extends keyof T>(keys: K[], obj: T): Pick<T, K>;
4+
//// const { /**/ } = pick(['b'], { a: 'a', b: 'b' });
5+
6+
verify.completions({
7+
marker: "",
8+
exact: ["b"]
9+
});

0 commit comments

Comments
 (0)