Skip to content

Commit be618b1

Browse files
authored
Stop looking at binding patterns for type argument inference (#45719)
* Ignore empty binding patterns for contextual typing * Stop looking at binding patterns for type argument inference entirely
1 parent bac841e commit be618b1

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed

src/compiler/checker.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -25314,7 +25314,8 @@ namespace ts {
2531425314
if (result) {
2531525315
return result;
2531625316
}
25317-
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
25317+
if (!(contextFlags! & ContextFlags.SkipBindingPatterns) && isBindingPattern(declaration.name)) {
25318+
// This is less a contextual type and more an implied shape - in some cases, this may be undesirable
2531825319
return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false);
2531925320
}
2532025321
}
@@ -28613,7 +28614,7 @@ namespace ts {
2861328614
// 'let f: (x: string) => number = wrap(s => s.length)', we infer from the declared type of 'f' to the
2861428615
// return type of 'wrap'.
2861528616
if (node.kind !== SyntaxKind.Decorator) {
28616-
const contextualType = getContextualType(node, every(signature.typeParameters, p => !!getDefaultFromTypeParameter(p)) ? ContextFlags.SkipBindingPatterns : ContextFlags.None);
28617+
const contextualType = getContextualType(node, ContextFlags.SkipBindingPatterns);
2861728618
if (contextualType) {
2861828619
// We clone the inference context to avoid disturbing a resolution in progress for an
2861928620
// outer call expression. Effectively we just want a snapshot of whatever has been

tests/baselines/reference/destructuringTuple.errors.txt

+1-19
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

+5-5
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[]
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)