Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a2b785b

Browse files
authoredMay 24, 2022
Limit type argument inference from binding patterns (microsoft#49086)
* WIP * Don’t widen literals based on bogus contextual type instantiation * Split tests * Skip unnecessary inference pass * Accept test baselines * Fix stray edit * Fix type mapper combination * Revert src/ of 7dc1952 * Make empty binding pattern provide no contextual type * Add missed baseline
1 parent 194a2ae commit a2b785b

29 files changed

+604
-97
lines changed
 

‎src/compiler/checker.ts

+42-30
Original file line numberDiff line numberDiff line change
@@ -26519,7 +26519,7 @@ namespace ts {
2651926519
if (result) {
2652026520
return result;
2652126521
}
26522-
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
26522+
if (!(contextFlags! & ContextFlags.SkipBindingPatterns) && isBindingPattern(declaration.name) && declaration.name.elements.length > 0) {
2652326523
return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false);
2652426524
}
2652526525
}
@@ -27053,22 +27053,20 @@ namespace ts {
2705327053
const inferenceContext = getInferenceContext(node);
2705427054
// If no inferences have been made, nothing is gained from instantiating as type parameters
2705527055
// would just be replaced with their defaults similar to the apparent type.
27056-
if (inferenceContext && some(inferenceContext.inferences, hasInferenceCandidates)) {
27056+
if (inferenceContext && contextFlags! & ContextFlags.Signature && some(inferenceContext.inferences, hasInferenceCandidates)) {
2705727057
// For contextual signatures we incorporate all inferences made so far, e.g. from return
2705827058
// types as well as arguments to the left in a function call.
27059-
if (contextFlags && contextFlags & ContextFlags.Signature) {
27060-
return instantiateInstantiableTypes(contextualType, inferenceContext.nonFixingMapper);
27061-
}
27059+
return instantiateInstantiableTypes(contextualType, inferenceContext.nonFixingMapper);
27060+
}
27061+
if (inferenceContext?.returnMapper) {
2706227062
// For other purposes (e.g. determining whether to produce literal types) we only
2706327063
// incorporate inferences made from the return type in a function call. We remove
2706427064
// the 'boolean' type from the contextual type such that contextually typed boolean
2706527065
// literals actually end up widening to 'boolean' (see #48363).
27066-
if (inferenceContext.returnMapper) {
27067-
const type = instantiateInstantiableTypes(contextualType, inferenceContext.returnMapper);
27068-
return type.flags & TypeFlags.Union && containsType((type as UnionType).types, regularFalseType) && containsType((type as UnionType).types, regularTrueType) ?
27069-
filterType(type, t => t !== regularFalseType && t !== regularTrueType) :
27070-
type;
27071-
}
27066+
const type = instantiateInstantiableTypes(contextualType, inferenceContext.returnMapper);
27067+
return type.flags & TypeFlags.Union && containsType((type as UnionType).types, regularFalseType) && containsType((type as UnionType).types, regularTrueType) ?
27068+
filterType(type, t => t !== regularFalseType && t !== regularTrueType) :
27069+
type;
2707227070
}
2707327071
}
2707427072
return contextualType;
@@ -29904,29 +29902,43 @@ namespace ts {
2990429902
// 'let f: (x: string) => number = wrap(s => s.length)', we infer from the declared type of 'f' to the
2990529903
// return type of 'wrap'.
2990629904
if (node.kind !== SyntaxKind.Decorator) {
29907-
const contextualType = getContextualType(node, every(signature.typeParameters, p => !!getDefaultFromTypeParameter(p)) ? ContextFlags.SkipBindingPatterns : ContextFlags.None);
29905+
const skipBindingPatterns = every(signature.typeParameters, p => !!getDefaultFromTypeParameter(p));
29906+
const contextualType = getContextualType(node, skipBindingPatterns ? ContextFlags.SkipBindingPatterns : ContextFlags.None);
2990829907
if (contextualType) {
2990929908
const inferenceTargetType = getReturnTypeOfSignature(signature);
2991029909
if (couldContainTypeVariables(inferenceTargetType)) {
29911-
// We clone the inference context to avoid disturbing a resolution in progress for an
29912-
// outer call expression. Effectively we just want a snapshot of whatever has been
29913-
// inferred for any outer call expression so far.
2991429910
const outerContext = getInferenceContext(node);
29915-
const outerMapper = getMapperFromContext(cloneInferenceContext(outerContext, InferenceFlags.NoDefault));
29916-
const instantiatedType = instantiateType(contextualType, outerMapper);
29917-
// If the contextual type is a generic function type with a single call signature, we
29918-
// instantiate the type with its own type parameters and type arguments. This ensures that
29919-
// the type parameters are not erased to type any during type inference such that they can
29920-
// be inferred as actual types from the contextual type. For example:
29921-
// declare function arrayMap<T, U>(f: (x: T) => U): (a: T[]) => U[];
29922-
// const boxElements: <A>(a: A[]) => { value: A }[] = arrayMap(value => ({ value }));
29923-
// Above, the type of the 'value' parameter is inferred to be 'A'.
29924-
const contextualSignature = getSingleCallSignature(instantiatedType);
29925-
const inferenceSourceType = contextualSignature && contextualSignature.typeParameters ?
29926-
getOrCreateTypeFromSignature(getSignatureInstantiationWithoutFillingInTypeArguments(contextualSignature, contextualSignature.typeParameters)) :
29927-
instantiatedType;
29928-
// Inferences made from return types have lower priority than all other inferences.
29929-
inferTypes(context.inferences, inferenceSourceType, inferenceTargetType, InferencePriority.ReturnType);
29911+
const isFromBindingPattern = !skipBindingPatterns && getContextualType(node, ContextFlags.SkipBindingPatterns) !== contextualType;
29912+
// A return type inference from a binding pattern can be used in instantiating the contextual
29913+
// type of an argument later in inference, but cannot stand on its own as the final return type.
29914+
// It is incorporated into `context.returnMapper` which is used in `instantiateContextualType`,
29915+
// but doesn't need to go into `context.inferences`. This allows a an array binding pattern to
29916+
// produce a tuple for `T` in
29917+
// declare function f<T>(cb: () => T): T;
29918+
// const [e1, e2, e3] = f(() => [1, "hi", true]);
29919+
// but does not produce any inference for `T` in
29920+
// declare function f<T>(): T;
29921+
// const [e1, e2, e3] = f();
29922+
if (!isFromBindingPattern) {
29923+
// We clone the inference context to avoid disturbing a resolution in progress for an
29924+
// outer call expression. Effectively we just want a snapshot of whatever has been
29925+
// inferred for any outer call expression so far.
29926+
const outerMapper = getMapperFromContext(cloneInferenceContext(outerContext, InferenceFlags.NoDefault));
29927+
const instantiatedType = instantiateType(contextualType, outerMapper);
29928+
// If the contextual type is a generic function type with a single call signature, we
29929+
// instantiate the type with its own type parameters and type arguments. This ensures that
29930+
// the type parameters are not erased to type any during type inference such that they can
29931+
// be inferred as actual types from the contextual type. For example:
29932+
// declare function arrayMap<T, U>(f: (x: T) => U): (a: T[]) => U[];
29933+
// const boxElements: <A>(a: A[]) => { value: A }[] = arrayMap(value => ({ value }));
29934+
// Above, the type of the 'value' parameter is inferred to be 'A'.
29935+
const contextualSignature = getSingleCallSignature(instantiatedType);
29936+
const inferenceSourceType = contextualSignature && contextualSignature.typeParameters ?
29937+
getOrCreateTypeFromSignature(getSignatureInstantiationWithoutFillingInTypeArguments(contextualSignature, contextualSignature.typeParameters)) :
29938+
instantiatedType;
29939+
// Inferences made from return types have lower priority than all other inferences.
29940+
inferTypes(context.inferences, inferenceSourceType, inferenceTargetType, InferencePriority.ReturnType);
29941+
}
2993029942
// Create a type mapper for instantiating generic contextual types using the inferences made
2993129943
// from the return type. We need a separate inference pass here because (a) instantiation of
2993229944
// the source type uses the outer context's return mapper (which excludes inferences made from

‎src/compiler/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5874,7 +5874,7 @@ namespace ts {
58745874
ReturnType = 1 << 7, // Inference made from return type of generic function
58755875
LiteralKeyof = 1 << 8, // Inference made from a string literal to a keyof T
58765876
NoConstraints = 1 << 9, // Don't infer from constraints of instantiable types
5877-
AlwaysStrict = 1 << 10, // Always use strict rules for contravariant inferences
5877+
AlwaysStrict = 1 << 10, // Always use strict rules for contravariant inferences
58785878
MaxValue = 1 << 11, // Seed for inference priority tracking
58795879

58805880
PriorityImpliesCombination = ReturnType | MappedTypeConstraint | LiteralKeyof, // These priorities imply that the resulting type should be a combination of all candidates
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts(2,7): error TS2571: Object is of type 'unknown'.
2+
tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts(3,9): error TS2339: Property 'p1' does not exist on type 'unknown'.
3+
tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts(4,7): error TS2461: Type 'unknown' is not an array type.
4+
tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts(4,7): error TS2571: Object is of type 'unknown'.
5+
tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts(5,7): error TS2461: Type 'unknown' is not an array type.
6+
7+
8+
==== tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts (5 errors) ====
9+
declare function f<T>(): T;
10+
const {} = f(); // error (only in strictNullChecks)
11+
~~
12+
!!! error TS2571: Object is of type 'unknown'.
13+
const { p1 } = f(); // error
14+
~~
15+
!!! error TS2339: Property 'p1' does not exist on type 'unknown'.
16+
const [] = f(); // error
17+
~~
18+
!!! error TS2461: Type 'unknown' is not an array type.
19+
~~
20+
!!! error TS2571: Object is of type 'unknown'.
21+
const [e1, e2] = f(); // error
22+
~~~~~~~~
23+
!!! error TS2461: Type 'unknown' is not an array type.
24+
25+
// Repro from #43605
26+
type Dispatch<A = { type: any; [extraProps: string]: any }> = { <T extends A>(action: T): T };
27+
type IFuncs = { readonly [key: string]: (...p: any) => void };
28+
type IDestructuring<T extends IFuncs> = { readonly [key in keyof T]?: (...p: Parameters<T[key]>) => void };
29+
type Destructuring<T extends IFuncs, U extends IDestructuring<T>> = (dispatch: Dispatch<any>, funcs: T) => U;
30+
const funcs1 = {
31+
funcA: (a: boolean): void => {},
32+
funcB: (b: string, bb: string): void => {},
33+
funcC: (c: number, cc: number, ccc: boolean): void => {},
34+
};
35+
type TFuncs1 = typeof funcs1;
36+
declare function useReduxDispatch1<T extends IDestructuring<TFuncs1>>(destructuring: Destructuring<TFuncs1, T>): T;
37+
const {} = useReduxDispatch1(
38+
(d, f) => ({
39+
funcA: (...p) => d(f.funcA(...p)), // p should be inferrable
40+
funcB: (...p) => d(f.funcB(...p)),
41+
funcC: (...p) => d(f.funcC(...p)),
42+
})
43+
);
44+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//// [bindingPatternCannotBeOnlyInferenceSource.ts]
2+
declare function f<T>(): T;
3+
const {} = f(); // error (only in strictNullChecks)
4+
const { p1 } = f(); // error
5+
const [] = f(); // error
6+
const [e1, e2] = f(); // error
7+
8+
// Repro from #43605
9+
type Dispatch<A = { type: any; [extraProps: string]: any }> = { <T extends A>(action: T): T };
10+
type IFuncs = { readonly [key: string]: (...p: any) => void };
11+
type IDestructuring<T extends IFuncs> = { readonly [key in keyof T]?: (...p: Parameters<T[key]>) => void };
12+
type Destructuring<T extends IFuncs, U extends IDestructuring<T>> = (dispatch: Dispatch<any>, funcs: T) => U;
13+
const funcs1 = {
14+
funcA: (a: boolean): void => {},
15+
funcB: (b: string, bb: string): void => {},
16+
funcC: (c: number, cc: number, ccc: boolean): void => {},
17+
};
18+
type TFuncs1 = typeof funcs1;
19+
declare function useReduxDispatch1<T extends IDestructuring<TFuncs1>>(destructuring: Destructuring<TFuncs1, T>): T;
20+
const {} = useReduxDispatch1(
21+
(d, f) => ({
22+
funcA: (...p) => d(f.funcA(...p)), // p should be inferrable
23+
funcB: (...p) => d(f.funcB(...p)),
24+
funcC: (...p) => d(f.funcC(...p)),
25+
})
26+
);
27+
28+
29+
//// [bindingPatternCannotBeOnlyInferenceSource.js]
30+
var _a = f(); // error (only in strictNullChecks)
31+
var p1 = f().p1; // error
32+
var _b = f(); // error
33+
var _c = f(), e1 = _c[0], e2 = _c[1]; // error
34+
var funcs1 = {
35+
funcA: function (a) { },
36+
funcB: function (b, bb) { },
37+
funcC: function (c, cc, ccc) { }
38+
};
39+
var _d = useReduxDispatch1(function (d, f) { return ({
40+
funcA: function () {
41+
var p = [];
42+
for (var _i = 0; _i < arguments.length; _i++) {
43+
p[_i] = arguments[_i];
44+
}
45+
return d(f.funcA.apply(f, p));
46+
},
47+
funcB: function () {
48+
var p = [];
49+
for (var _i = 0; _i < arguments.length; _i++) {
50+
p[_i] = arguments[_i];
51+
}
52+
return d(f.funcB.apply(f, p));
53+
},
54+
funcC: function () {
55+
var p = [];
56+
for (var _i = 0; _i < arguments.length; _i++) {
57+
p[_i] = arguments[_i];
58+
}
59+
return d(f.funcC.apply(f, p));
60+
}
61+
}); });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
=== tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts ===
2+
declare function f<T>(): T;
3+
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 0, 0))
4+
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 0, 19))
5+
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 0, 19))
6+
7+
const {} = f(); // error (only in strictNullChecks)
8+
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 0, 0))
9+
10+
const { p1 } = f(); // error
11+
>p1 : Symbol(p1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 2, 7))
12+
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 0, 0))
13+
14+
const [] = f(); // error
15+
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 0, 0))
16+
17+
const [e1, e2] = f(); // error
18+
>e1 : Symbol(e1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 4, 7))
19+
>e2 : Symbol(e2, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 4, 10))
20+
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 0, 0))
21+
22+
// Repro from #43605
23+
type Dispatch<A = { type: any; [extraProps: string]: any }> = { <T extends A>(action: T): T };
24+
>Dispatch : Symbol(Dispatch, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 4, 21))
25+
>A : Symbol(A, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 14))
26+
>type : Symbol(type, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 19))
27+
>extraProps : Symbol(extraProps, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 32))
28+
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 65))
29+
>A : Symbol(A, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 14))
30+
>action : Symbol(action, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 78))
31+
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 65))
32+
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 65))
33+
34+
type IFuncs = { readonly [key: string]: (...p: any) => void };
35+
>IFuncs : Symbol(IFuncs, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 94))
36+
>key : Symbol(key, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 8, 26))
37+
>p : Symbol(p, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 8, 41))
38+
39+
type IDestructuring<T extends IFuncs> = { readonly [key in keyof T]?: (...p: Parameters<T[key]>) => void };
40+
>IDestructuring : Symbol(IDestructuring, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 8, 62))
41+
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 9, 20))
42+
>IFuncs : Symbol(IFuncs, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 94))
43+
>key : Symbol(key, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 9, 52))
44+
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 9, 20))
45+
>p : Symbol(p, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 9, 71))
46+
>Parameters : Symbol(Parameters, Decl(lib.es5.d.ts, --, --))
47+
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 9, 20))
48+
>key : Symbol(key, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 9, 52))
49+
50+
type Destructuring<T extends IFuncs, U extends IDestructuring<T>> = (dispatch: Dispatch<any>, funcs: T) => U;
51+
>Destructuring : Symbol(Destructuring, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 9, 107))
52+
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 10, 19))
53+
>IFuncs : Symbol(IFuncs, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 94))
54+
>U : Symbol(U, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 10, 36))
55+
>IDestructuring : Symbol(IDestructuring, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 8, 62))
56+
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 10, 19))
57+
>dispatch : Symbol(dispatch, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 10, 69))
58+
>Dispatch : Symbol(Dispatch, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 4, 21))
59+
>funcs : Symbol(funcs, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 10, 93))
60+
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 10, 19))
61+
>U : Symbol(U, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 10, 36))
62+
63+
const funcs1 = {
64+
>funcs1 : Symbol(funcs1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 11, 5))
65+
66+
funcA: (a: boolean): void => {},
67+
>funcA : Symbol(funcA, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 11, 16))
68+
>a : Symbol(a, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 12, 12))
69+
70+
funcB: (b: string, bb: string): void => {},
71+
>funcB : Symbol(funcB, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 12, 36))
72+
>b : Symbol(b, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 13, 12))
73+
>bb : Symbol(bb, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 13, 22))
74+
75+
funcC: (c: number, cc: number, ccc: boolean): void => {},
76+
>funcC : Symbol(funcC, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 13, 47))
77+
>c : Symbol(c, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 14, 12))
78+
>cc : Symbol(cc, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 14, 22))
79+
>ccc : Symbol(ccc, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 14, 34))
80+
81+
};
82+
type TFuncs1 = typeof funcs1;
83+
>TFuncs1 : Symbol(TFuncs1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 15, 2))
84+
>funcs1 : Symbol(funcs1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 11, 5))
85+
86+
declare function useReduxDispatch1<T extends IDestructuring<TFuncs1>>(destructuring: Destructuring<TFuncs1, T>): T;
87+
>useReduxDispatch1 : Symbol(useReduxDispatch1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 16, 29))
88+
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 17, 35))
89+
>IDestructuring : Symbol(IDestructuring, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 8, 62))
90+
>TFuncs1 : Symbol(TFuncs1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 15, 2))
91+
>destructuring : Symbol(destructuring, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 17, 70))
92+
>Destructuring : Symbol(Destructuring, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 9, 107))
93+
>TFuncs1 : Symbol(TFuncs1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 15, 2))
94+
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 17, 35))
95+
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 17, 35))
96+
97+
const {} = useReduxDispatch1(
98+
>useReduxDispatch1 : Symbol(useReduxDispatch1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 16, 29))
99+
100+
(d, f) => ({
101+
>d : Symbol(d, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 5))
102+
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 7))
103+
104+
funcA: (...p) => d(f.funcA(...p)), // p should be inferrable
105+
>funcA : Symbol(funcA, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 16))
106+
>p : Symbol(p, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 20, 16))
107+
>d : Symbol(d, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 5))
108+
>f.funcA : Symbol(funcA, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 11, 16))
109+
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 7))
110+
>funcA : Symbol(funcA, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 11, 16))
111+
>p : Symbol(p, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 20, 16))
112+
113+
funcB: (...p) => d(f.funcB(...p)),
114+
>funcB : Symbol(funcB, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 20, 42))
115+
>p : Symbol(p, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 21, 16))
116+
>d : Symbol(d, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 5))
117+
>f.funcB : Symbol(funcB, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 12, 36))
118+
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 7))
119+
>funcB : Symbol(funcB, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 12, 36))
120+
>p : Symbol(p, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 21, 16))
121+
122+
funcC: (...p) => d(f.funcC(...p)),
123+
>funcC : Symbol(funcC, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 21, 42))
124+
>p : Symbol(p, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 22, 16))
125+
>d : Symbol(d, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 5))
126+
>f.funcC : Symbol(funcC, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 13, 47))
127+
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 7))
128+
>funcC : Symbol(funcC, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 13, 47))
129+
>p : Symbol(p, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 22, 16))
130+
131+
})
132+
);
133+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
=== tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts ===
2+
declare function f<T>(): T;
3+
>f : <T>() => T
4+
5+
const {} = f(); // error (only in strictNullChecks)
6+
>f() : unknown
7+
>f : <T>() => T
8+
9+
const { p1 } = f(); // error
10+
>p1 : any
11+
>f() : unknown
12+
>f : <T>() => T
13+
14+
const [] = f(); // error
15+
>f() : unknown
16+
>f : <T>() => T
17+
18+
const [e1, e2] = f(); // error
19+
>e1 : any
20+
>e2 : any
21+
>f() : unknown
22+
>f : <T>() => T
23+
24+
// Repro from #43605
25+
type Dispatch<A = { type: any; [extraProps: string]: any }> = { <T extends A>(action: T): T };
26+
>Dispatch : Dispatch<A>
27+
>type : any
28+
>extraProps : string
29+
>action : T
30+
31+
type IFuncs = { readonly [key: string]: (...p: any) => void };
32+
>IFuncs : { readonly [key: string]: (...p: any) => void; }
33+
>key : string
34+
>p : any
35+
36+
type IDestructuring<T extends IFuncs> = { readonly [key in keyof T]?: (...p: Parameters<T[key]>) => void };
37+
>IDestructuring : IDestructuring<T>
38+
>p : Parameters<T[key]>
39+
40+
type Destructuring<T extends IFuncs, U extends IDestructuring<T>> = (dispatch: Dispatch<any>, funcs: T) => U;
41+
>Destructuring : Destructuring<T, U>
42+
>dispatch : Dispatch<any>
43+
>funcs : T
44+
45+
const funcs1 = {
46+
>funcs1 : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
47+
>{ funcA: (a: boolean): void => {}, funcB: (b: string, bb: string): void => {}, funcC: (c: number, cc: number, ccc: boolean): void => {},} : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
48+
49+
funcA: (a: boolean): void => {},
50+
>funcA : (a: boolean) => void
51+
>(a: boolean): void => {} : (a: boolean) => void
52+
>a : boolean
53+
54+
funcB: (b: string, bb: string): void => {},
55+
>funcB : (b: string, bb: string) => void
56+
>(b: string, bb: string): void => {} : (b: string, bb: string) => void
57+
>b : string
58+
>bb : string
59+
60+
funcC: (c: number, cc: number, ccc: boolean): void => {},
61+
>funcC : (c: number, cc: number, ccc: boolean) => void
62+
>(c: number, cc: number, ccc: boolean): void => {} : (c: number, cc: number, ccc: boolean) => void
63+
>c : number
64+
>cc : number
65+
>ccc : boolean
66+
67+
};
68+
type TFuncs1 = typeof funcs1;
69+
>TFuncs1 : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
70+
>funcs1 : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
71+
72+
declare function useReduxDispatch1<T extends IDestructuring<TFuncs1>>(destructuring: Destructuring<TFuncs1, T>): T;
73+
>useReduxDispatch1 : <T extends IDestructuring<{ funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }>>(destructuring: Destructuring<TFuncs1, T>) => T
74+
>destructuring : Destructuring<{ funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }, T>
75+
76+
const {} = useReduxDispatch1(
77+
>useReduxDispatch1( (d, f) => ({ funcA: (...p) => d(f.funcA(...p)), // p should be inferrable funcB: (...p) => d(f.funcB(...p)), funcC: (...p) => d(f.funcC(...p)), })) : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
78+
>useReduxDispatch1 : <T extends IDestructuring<{ funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }>>(destructuring: Destructuring<{ funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }, T>) => T
79+
80+
(d, f) => ({
81+
>(d, f) => ({ funcA: (...p) => d(f.funcA(...p)), // p should be inferrable funcB: (...p) => d(f.funcB(...p)), funcC: (...p) => d(f.funcC(...p)), }) : (d: Dispatch<any>, f: { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }) => { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
82+
>d : Dispatch<any>
83+
>f : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
84+
>({ funcA: (...p) => d(f.funcA(...p)), // p should be inferrable funcB: (...p) => d(f.funcB(...p)), funcC: (...p) => d(f.funcC(...p)), }) : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
85+
>{ funcA: (...p) => d(f.funcA(...p)), // p should be inferrable funcB: (...p) => d(f.funcB(...p)), funcC: (...p) => d(f.funcC(...p)), } : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
86+
87+
funcA: (...p) => d(f.funcA(...p)), // p should be inferrable
88+
>funcA : (a: boolean) => void
89+
>(...p) => d(f.funcA(...p)) : (a: boolean) => void
90+
>p : [a: boolean]
91+
>d(f.funcA(...p)) : void
92+
>d : Dispatch<any>
93+
>f.funcA(...p) : void
94+
>f.funcA : (a: boolean) => void
95+
>f : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
96+
>funcA : (a: boolean) => void
97+
>...p : boolean
98+
>p : [a: boolean]
99+
100+
funcB: (...p) => d(f.funcB(...p)),
101+
>funcB : (b: string, bb: string) => void
102+
>(...p) => d(f.funcB(...p)) : (b: string, bb: string) => void
103+
>p : [b: string, bb: string]
104+
>d(f.funcB(...p)) : void
105+
>d : Dispatch<any>
106+
>f.funcB(...p) : void
107+
>f.funcB : (b: string, bb: string) => void
108+
>f : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
109+
>funcB : (b: string, bb: string) => void
110+
>...p : string
111+
>p : [b: string, bb: string]
112+
113+
funcC: (...p) => d(f.funcC(...p)),
114+
>funcC : (c: number, cc: number, ccc: boolean) => void
115+
>(...p) => d(f.funcC(...p)) : (c: number, cc: number, ccc: boolean) => void
116+
>p : [c: number, cc: number, ccc: boolean]
117+
>d(f.funcC(...p)) : void
118+
>d : Dispatch<any>
119+
>f.funcC(...p) : void
120+
>f.funcC : (c: number, cc: number, ccc: boolean) => void
121+
>f : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
122+
>funcC : (c: number, cc: number, ccc: boolean) => void
123+
>...p : number | boolean
124+
>p : [c: number, cc: number, ccc: boolean]
125+
126+
})
127+
);
128+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [bindingPatternContextualTypeDoesNotCauseWidening.ts]
2+
declare function pick<O, T extends keyof O>(keys: T[], obj?: O): Pick<O, T>;
3+
const _ = pick(['b'], { a: 'a', b: 'b' }); // T: "b"
4+
const { } = pick(['b'], { a: 'a', b: 'b' }); // T: "b" | "a" ??? (before fix)
5+
6+
7+
//// [bindingPatternContextualTypeDoesNotCauseWidening.js]
8+
var _ = pick(['b'], { a: 'a', b: 'b' }); // T: "b"
9+
var _a = pick(['b'], { a: 'a', b: 'b' }); // T: "b" | "a" ??? (before fix)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/bindingPatternContextualTypeDoesNotCauseWidening.ts ===
2+
declare function pick<O, T extends keyof O>(keys: T[], obj?: O): Pick<O, T>;
3+
>pick : Symbol(pick, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 0))
4+
>O : Symbol(O, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 22))
5+
>T : Symbol(T, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 24))
6+
>O : Symbol(O, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 22))
7+
>keys : Symbol(keys, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 44))
8+
>T : Symbol(T, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 24))
9+
>obj : Symbol(obj, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 54))
10+
>O : Symbol(O, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 22))
11+
>Pick : Symbol(Pick, Decl(lib.es5.d.ts, --, --))
12+
>O : Symbol(O, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 22))
13+
>T : Symbol(T, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 24))
14+
15+
const _ = pick(['b'], { a: 'a', b: 'b' }); // T: "b"
16+
>_ : Symbol(_, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 1, 5))
17+
>pick : Symbol(pick, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 0))
18+
>a : Symbol(a, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 1, 26))
19+
>b : Symbol(b, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 1, 34))
20+
21+
const { } = pick(['b'], { a: 'a', b: 'b' }); // T: "b" | "a" ??? (before fix)
22+
>pick : Symbol(pick, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 0))
23+
>a : Symbol(a, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 2, 26))
24+
>b : Symbol(b, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 2, 34))
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/bindingPatternContextualTypeDoesNotCauseWidening.ts ===
2+
declare function pick<O, T extends keyof O>(keys: T[], obj?: O): Pick<O, T>;
3+
>pick : <O, T extends keyof O>(keys: T[], obj?: O) => Pick<O, T>
4+
>keys : T[]
5+
>obj : O
6+
7+
const _ = pick(['b'], { a: 'a', b: 'b' }); // T: "b"
8+
>_ : Pick<{ a: string; b: string; }, "b">
9+
>pick(['b'], { a: 'a', b: 'b' }) : Pick<{ a: string; b: string; }, "b">
10+
>pick : <O, T extends keyof O>(keys: T[], obj?: O) => Pick<O, T>
11+
>['b'] : "b"[]
12+
>'b' : "b"
13+
>{ a: 'a', b: 'b' } : { a: string; b: string; }
14+
>a : string
15+
>'a' : "a"
16+
>b : string
17+
>'b' : "b"
18+
19+
const { } = pick(['b'], { a: 'a', b: 'b' }); // T: "b" | "a" ??? (before fix)
20+
>pick(['b'], { a: 'a', b: 'b' }) : Pick<{ a: string; b: string; }, "b">
21+
>pick : <O, T extends keyof O>(keys: T[], obj?: O) => Pick<O, T>
22+
>['b'] : "b"[]
23+
>'b' : "b"
24+
>{ a: 'a', b: 'b' } : { a: string; b: string; }
25+
>a : string
26+
>'a' : "a"
27+
>b : string
28+
>'b' : "b"
29+

‎tests/baselines/reference/declarationEmitDestructuring4.errors.txt

-17
This file was deleted.

‎tests/baselines/reference/declarationEmitDestructuringObjectLiteralPattern.errors.txt

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

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

+2-8
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"];
@@ -46,11 +44,7 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9):
4644
}
4745

4846
function f2() {
49-
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 '{}'.
47+
var { } = { x: 5, y: "hello" }; // Ok, empty binding pattern means nothing
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/declarationsAndAssignments.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function f1() {
2020
}
2121

2222
function f2() {
23-
var { } = { x: 5, y: "hello" }; // Error, no x and y in target
23+
var { } = { x: 5, y: "hello" }; // Ok, empty binding pattern means nothing
2424
var { x } = { x: 5, y: "hello" }; // Error, no y in target
2525
var { y } = { x: 5, y: "hello" }; // Error, no x in target
2626
var { x, y } = { x: 5, y: "hello" };
@@ -206,7 +206,7 @@ function f1() {
206206
var z;
207207
}
208208
function f2() {
209-
var _a = { x: 5, y: "hello" }; // Error, no x and y in target
209+
var _a = { x: 5, y: "hello" }; // Ok, empty binding pattern means nothing
210210
var x = { x: 5, y: "hello" }.x; // Error, no y in target
211211
var y = { x: 5, y: "hello" }.y; // Error, no x in target
212212
var _b = { x: 5, y: "hello" }, x = _b.x, y = _b.y;

‎tests/baselines/reference/declarationsAndAssignments.symbols

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function f1() {
5959
function f2() {
6060
>f2 : Symbol(f2, Decl(declarationsAndAssignments.ts, 18, 1))
6161

62-
var { } = { x: 5, y: "hello" }; // Error, no x and y in target
62+
var { } = { x: 5, y: "hello" }; // Ok, empty binding pattern means nothing
6363
>x : Symbol(x, Decl(declarationsAndAssignments.ts, 21, 15))
6464
>y : Symbol(y, Decl(declarationsAndAssignments.ts, 21, 21))
6565

‎tests/baselines/reference/declarationsAndAssignments.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function f1() {
8181
function f2() {
8282
>f2 : () => void
8383

84-
var { } = { x: 5, y: "hello" }; // Error, no x and y in target
84+
var { } = { x: 5, y: "hello" }; // Ok, empty binding pattern means nothing
8585
>{ x: 5, y: "hello" } : { x: number; y: string; }
8686
>x : number
8787
>5 : 5

‎tests/baselines/reference/emptyObjectBindingPatternParameter04.errors.txt

-15
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [inferTupleFromBindingPattern.ts]
2+
declare function f<T>(cb: () => T): T;
3+
const [e1, e2, e3] = f(() => [1, "hi", true]);
4+
5+
6+
//// [inferTupleFromBindingPattern.js]
7+
var _a = f(function () { return [1, "hi", true]; }), e1 = _a[0], e2 = _a[1], e3 = _a[2];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/inferTupleFromBindingPattern.ts ===
2+
declare function f<T>(cb: () => T): T;
3+
>f : Symbol(f, Decl(inferTupleFromBindingPattern.ts, 0, 0))
4+
>T : Symbol(T, Decl(inferTupleFromBindingPattern.ts, 0, 19))
5+
>cb : Symbol(cb, Decl(inferTupleFromBindingPattern.ts, 0, 22))
6+
>T : Symbol(T, Decl(inferTupleFromBindingPattern.ts, 0, 19))
7+
>T : Symbol(T, Decl(inferTupleFromBindingPattern.ts, 0, 19))
8+
9+
const [e1, e2, e3] = f(() => [1, "hi", true]);
10+
>e1 : Symbol(e1, Decl(inferTupleFromBindingPattern.ts, 1, 7))
11+
>e2 : Symbol(e2, Decl(inferTupleFromBindingPattern.ts, 1, 10))
12+
>e3 : Symbol(e3, Decl(inferTupleFromBindingPattern.ts, 1, 14))
13+
>f : Symbol(f, Decl(inferTupleFromBindingPattern.ts, 0, 0))
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/inferTupleFromBindingPattern.ts ===
2+
declare function f<T>(cb: () => T): T;
3+
>f : <T>(cb: () => T) => T
4+
>cb : () => T
5+
6+
const [e1, e2, e3] = f(() => [1, "hi", true]);
7+
>e1 : number
8+
>e2 : string
9+
>e3 : boolean
10+
>f(() => [1, "hi", true]) : [number, string, boolean]
11+
>f : <T>(cb: () => T) => T
12+
>() => [1, "hi", true] : () => [number, string, boolean]
13+
>[1, "hi", true] : [number, string, true]
14+
>1 : 1
15+
>"hi" : "hi"
16+
>true : true
17+

‎tests/baselines/reference/missingAndExcessProperties.errors.txt

+1-7
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; }'.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [objectBindingPatternContextuallyTypesArgument.ts]
2+
declare function id<T>(x: T): T;
3+
const { f = (x: string) => x.length } = id({ f: x => x.charAt });
4+
5+
6+
//// [objectBindingPatternContextuallyTypesArgument.js]
7+
var _a = id({ f: function (x) { return x.charAt; } }).f, f = _a === void 0 ? function (x) { return x.length; } : _a;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/objectBindingPatternContextuallyTypesArgument.ts ===
2+
declare function id<T>(x: T): T;
3+
>id : Symbol(id, Decl(objectBindingPatternContextuallyTypesArgument.ts, 0, 0))
4+
>T : Symbol(T, Decl(objectBindingPatternContextuallyTypesArgument.ts, 0, 20))
5+
>x : Symbol(x, Decl(objectBindingPatternContextuallyTypesArgument.ts, 0, 23))
6+
>T : Symbol(T, Decl(objectBindingPatternContextuallyTypesArgument.ts, 0, 20))
7+
>T : Symbol(T, Decl(objectBindingPatternContextuallyTypesArgument.ts, 0, 20))
8+
9+
const { f = (x: string) => x.length } = id({ f: x => x.charAt });
10+
>f : Symbol(f, Decl(objectBindingPatternContextuallyTypesArgument.ts, 1, 7))
11+
>x : Symbol(x, Decl(objectBindingPatternContextuallyTypesArgument.ts, 1, 13))
12+
>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
13+
>x : Symbol(x, Decl(objectBindingPatternContextuallyTypesArgument.ts, 1, 13))
14+
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
15+
>id : Symbol(id, Decl(objectBindingPatternContextuallyTypesArgument.ts, 0, 0))
16+
>f : Symbol(f, Decl(objectBindingPatternContextuallyTypesArgument.ts, 1, 44))
17+
>x : Symbol(x, Decl(objectBindingPatternContextuallyTypesArgument.ts, 1, 47))
18+
>x.charAt : Symbol(String.charAt, Decl(lib.es5.d.ts, --, --))
19+
>x : Symbol(x, Decl(objectBindingPatternContextuallyTypesArgument.ts, 1, 47))
20+
>charAt : Symbol(String.charAt, Decl(lib.es5.d.ts, --, --))
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/objectBindingPatternContextuallyTypesArgument.ts ===
2+
declare function id<T>(x: T): T;
3+
>id : <T>(x: T) => T
4+
>x : T
5+
6+
const { f = (x: string) => x.length } = id({ f: x => x.charAt });
7+
>f : ((x: string) => number) | ((x: string) => (pos: number) => string)
8+
>(x: string) => x.length : (x: string) => number
9+
>x : string
10+
>x.length : number
11+
>x : string
12+
>length : number
13+
>id({ f: x => x.charAt }) : { f: (x: string) => (pos: number) => string; }
14+
>id : <T>(x: T) => T
15+
>{ f: x => x.charAt } : { f: (x: string) => (pos: number) => string; }
16+
>f : (x: string) => (pos: number) => string
17+
>x => x.charAt : (x: string) => (pos: number) => string
18+
>x : string
19+
>x.charAt : (pos: number) => string
20+
>x : string
21+
>charAt : (pos: number) => string
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// @strictNullChecks: true
2+
3+
declare function f<T>(): T;
4+
const {} = f(); // error (only in strictNullChecks)
5+
const { p1 } = f(); // error
6+
const [] = f(); // error
7+
const [e1, e2] = f(); // error
8+
9+
// Repro from #43605
10+
type Dispatch<A = { type: any; [extraProps: string]: any }> = { <T extends A>(action: T): T };
11+
type IFuncs = { readonly [key: string]: (...p: any) => void };
12+
type IDestructuring<T extends IFuncs> = { readonly [key in keyof T]?: (...p: Parameters<T[key]>) => void };
13+
type Destructuring<T extends IFuncs, U extends IDestructuring<T>> = (dispatch: Dispatch<any>, funcs: T) => U;
14+
const funcs1 = {
15+
funcA: (a: boolean): void => {},
16+
funcB: (b: string, bb: string): void => {},
17+
funcC: (c: number, cc: number, ccc: boolean): void => {},
18+
};
19+
type TFuncs1 = typeof funcs1;
20+
declare function useReduxDispatch1<T extends IDestructuring<TFuncs1>>(destructuring: Destructuring<TFuncs1, T>): T;
21+
const {} = useReduxDispatch1(
22+
(d, f) => ({
23+
funcA: (...p) => d(f.funcA(...p)), // p should be inferrable
24+
funcB: (...p) => d(f.funcB(...p)),
25+
funcC: (...p) => d(f.funcC(...p)),
26+
})
27+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare function pick<O, T extends keyof O>(keys: T[], obj?: O): Pick<O, T>;
2+
const _ = pick(['b'], { a: 'a', b: 'b' }); // T: "b"
3+
const { } = pick(['b'], { a: 'a', b: 'b' }); // T: "b" | "a" ??? (before fix)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare function f<T>(cb: () => T): T;
2+
const [e1, e2, e3] = f(() => [1, "hi", true]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare function id<T>(x: T): T;
2+
const { f = (x: string) => x.length } = id({ f: x => x.charAt });

‎tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function f1() {
1919
}
2020

2121
function f2() {
22-
var { } = { x: 5, y: "hello" }; // Error, no x and y in target
22+
var { } = { x: 5, y: "hello" }; // Ok, empty binding pattern means nothing
2323
var { x } = { x: 5, y: "hello" }; // Error, no y in target
2424
var { y } = { x: 5, y: "hello" }; // Error, no x in target
2525
var { x, y } = { x: 5, y: "hello" };

0 commit comments

Comments
 (0)
Please sign in to comment.