Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert binding pattern inference changes, but only for tuples #46009

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25442,7 +25442,8 @@ namespace ts {
if (result) {
return result;
}
if (!(contextFlags! & ContextFlags.SkipBindingPatterns) && isBindingPattern(declaration.name)) {
if (!(contextFlags! & ContextFlags.SkipObjectBindingPatterns) && isObjectBindingPattern(declaration.name) ||
!(contextFlags! & ContextFlags.SkipArrayBindingPatterns) && isArrayBindingPattern(declaration.name)) {
// This is less a contextual type and more an implied shape - in some cases, this may be undesirable
return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false);
}
Expand Down Expand Up @@ -28750,7 +28751,7 @@ namespace ts {
// 'let f: (x: string) => number = wrap(s => s.length)', we infer from the declared type of 'f' to the
// return type of 'wrap'.
if (node.kind !== SyntaxKind.Decorator) {
const contextualType = getContextualType(node, ContextFlags.SkipBindingPatterns);
const contextualType = getContextualType(node, ContextFlags.SkipObjectBindingPatterns);
if (contextualType) {
// We clone the inference context to avoid disturbing a resolution in progress for an
// outer call expression. Effectively we just want a snapshot of whatever has been
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4409,7 +4409,9 @@ namespace ts {
Signature = 1 << 0, // Obtaining contextual signature
NoConstraints = 1 << 1, // Don't obtain type variable constraints
Completions = 1 << 2, // Ignore inference to current node and parent nodes out to the containing call for completions
SkipBindingPatterns = 1 << 3, // Ignore contextual types applied by binding patterns
SkipArrayBindingPatterns = 1 << 3, // Ignore contextual types applied by array binding patterns
SkipObjectBindingPatterns = 1 << 4, // Ignore contextual types applied by object binding patterns
SkipBindingPatterns = SkipArrayBindingPatterns | SkipObjectBindingPatterns,
}

// NOTE: If modifying this enum, must modify `TypeFormatFlags` too!
Expand Down
20 changes: 19 additions & 1 deletion tests/baselines/reference/destructuringTuple.errors.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
tests/cases/compiler/destructuringTuple.ts(11,7): error TS2461: Type 'number' is not an array type.
tests/cases/compiler/destructuringTuple.ts(11,48): error TS2769: No overload matches this call.
Overload 1 of 3, '(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number', gave the following error.
Type 'never[]' is not assignable to type 'number'.
Overload 2 of 3, '(callbackfn: (previousValue: [], currentValue: number, currentIndex: number, array: number[]) => [], initialValue: []): []', gave the following error.
Type 'never[]' is not assignable to type '[]'.
Target allows only 0 element(s) but source may have more.
tests/cases/compiler/destructuringTuple.ts(11,60): error TS2769: No overload matches this call.
Overload 1 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
Argument of type 'number' is not assignable to parameter of type 'ConcatArray<never>'.
Overload 2 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
Argument of type 'number' is not assignable to parameter of type 'ConcatArray<never>'.


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

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

const [oops1] = [1, 2, 3].reduce((accu, el) => accu.concat(el), []);
~~~~~~~
!!! error TS2461: Type 'number' is not an array type.
~~~~~~~~~~~~~~~
!!! error TS2769: No overload matches this call.
!!! error TS2769: Overload 1 of 3, '(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number', gave the following error.
!!! error TS2769: Type 'never[]' is not assignable to type 'number'.
!!! error TS2769: Overload 2 of 3, '(callbackfn: (previousValue: [], currentValue: number, currentIndex: number, array: number[]) => [], initialValue: []): []', gave the following error.
!!! error TS2769: Type 'never[]' is not assignable to type '[]'.
!!! error TS2769: Target allows only 0 element(s) but source may have more.
!!! related TS6502 /.ts/lib.es5.d.ts:1429:24: The expected type comes from the return type of this signature.
!!! related TS6502 /.ts/lib.es5.d.ts:1435:27: The expected type comes from the return type of this signature.
~~
!!! error TS2769: No overload matches this call.
!!! error TS2769: Overload 1 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
Expand Down
10 changes: 5 additions & 5 deletions tests/baselines/reference/destructuringTuple.types
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ declare var receiver: typeof tuple;
// Repros from #32140

const [oops1] = [1, 2, 3].reduce((accu, el) => accu.concat(el), []);
>oops1 : never
>[1, 2, 3].reduce((accu, el) => accu.concat(el), []) : never[]
>oops1 : any
>[1, 2, 3].reduce((accu, el) => accu.concat(el), []) : number
>[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; }
>[1, 2, 3] : number[]
>1 : 1
>2 : 2
>3 : 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; }
>(accu, el) => accu.concat(el) : (accu: never[], el: number) => never[]
>accu : never[]
>(accu, el) => accu.concat(el) : (accu: [], el: number) => never[]
>accu : []
>el : number
>accu.concat(el) : never[]
>accu.concat : { (...items: ConcatArray<never>[]): never[]; (...items: ConcatArray<never>[]): never[]; }
>accu : never[]
>accu : []
>concat : { (...items: ConcatArray<never>[]): never[]; (...items: ConcatArray<never>[]): never[]; }
>el : number
>[] : never[]
Expand Down
7 changes: 7 additions & 0 deletions tests/baselines/reference/destructuringTuple2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//// [destructuringTuple2.ts]
declare const f: <T>(cb: () => T) => T;
const [a, b, c] = f(() => [1, "hi", true]);


//// [destructuringTuple2.js]
var _a = f(function () { return [1, "hi", true]; }), a = _a[0], b = _a[1], c = _a[2];
14 changes: 14 additions & 0 deletions tests/baselines/reference/destructuringTuple2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/compiler/destructuringTuple2.ts ===
declare const f: <T>(cb: () => T) => T;
>f : Symbol(f, Decl(destructuringTuple2.ts, 0, 13))
>T : Symbol(T, Decl(destructuringTuple2.ts, 0, 18))
>cb : Symbol(cb, Decl(destructuringTuple2.ts, 0, 21))
>T : Symbol(T, Decl(destructuringTuple2.ts, 0, 18))
>T : Symbol(T, Decl(destructuringTuple2.ts, 0, 18))

const [a, b, c] = f(() => [1, "hi", true]);
>a : Symbol(a, Decl(destructuringTuple2.ts, 1, 7))
>b : Symbol(b, Decl(destructuringTuple2.ts, 1, 9))
>c : Symbol(c, Decl(destructuringTuple2.ts, 1, 12))
>f : Symbol(f, Decl(destructuringTuple2.ts, 0, 13))

17 changes: 17 additions & 0 deletions tests/baselines/reference/destructuringTuple2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/destructuringTuple2.ts ===
declare const f: <T>(cb: () => T) => T;
>f : <T>(cb: () => T) => T
>cb : () => T

const [a, b, c] = f(() => [1, "hi", true]);
>a : number
>b : string
>c : boolean
>f(() => [1, "hi", true]) : [number, string, boolean]
>f : <T>(cb: () => T) => T
>() => [1, "hi", true] : () => [number, string, boolean]
>[1, "hi", true] : [number, string, true]
>1 : 1
>"hi" : "hi"
>true : true

2 changes: 2 additions & 0 deletions tests/cases/compiler/destructuringTuple2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const f: <T>(cb: () => T) => T;
const [a, b, c] = f(() => [1, "hi", true]);