Limit type argument inference from binding patterns (#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 7dc1952a82

* Make empty binding pattern provide no contextual type

* Add missed baseline
This commit is contained in:
Andrew Branch 2022-05-24 09:05:34 -07:00 committed by GitHub
parent 194a2aea0d
commit a2b785b6cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 604 additions and 97 deletions

View File

@ -26519,7 +26519,7 @@ namespace ts {
if (result) {
return result;
}
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
if (!(contextFlags! & ContextFlags.SkipBindingPatterns) && isBindingPattern(declaration.name) && declaration.name.elements.length > 0) {
return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false);
}
}
@ -27053,22 +27053,20 @@ namespace ts {
const inferenceContext = getInferenceContext(node);
// If no inferences have been made, nothing is gained from instantiating as type parameters
// would just be replaced with their defaults similar to the apparent type.
if (inferenceContext && some(inferenceContext.inferences, hasInferenceCandidates)) {
if (inferenceContext && contextFlags! & ContextFlags.Signature && some(inferenceContext.inferences, hasInferenceCandidates)) {
// For contextual signatures we incorporate all inferences made so far, e.g. from return
// types as well as arguments to the left in a function call.
if (contextFlags && contextFlags & ContextFlags.Signature) {
return instantiateInstantiableTypes(contextualType, inferenceContext.nonFixingMapper);
}
return instantiateInstantiableTypes(contextualType, inferenceContext.nonFixingMapper);
}
if (inferenceContext?.returnMapper) {
// For other purposes (e.g. determining whether to produce literal types) we only
// incorporate inferences made from the return type in a function call. We remove
// the 'boolean' type from the contextual type such that contextually typed boolean
// literals actually end up widening to 'boolean' (see #48363).
if (inferenceContext.returnMapper) {
const type = instantiateInstantiableTypes(contextualType, inferenceContext.returnMapper);
return type.flags & TypeFlags.Union && containsType((type as UnionType).types, regularFalseType) && containsType((type as UnionType).types, regularTrueType) ?
filterType(type, t => t !== regularFalseType && t !== regularTrueType) :
type;
}
const type = instantiateInstantiableTypes(contextualType, inferenceContext.returnMapper);
return type.flags & TypeFlags.Union && containsType((type as UnionType).types, regularFalseType) && containsType((type as UnionType).types, regularTrueType) ?
filterType(type, t => t !== regularFalseType && t !== regularTrueType) :
type;
}
}
return contextualType;
@ -29904,29 +29902,43 @@ 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, every(signature.typeParameters, p => !!getDefaultFromTypeParameter(p)) ? ContextFlags.SkipBindingPatterns : ContextFlags.None);
const skipBindingPatterns = every(signature.typeParameters, p => !!getDefaultFromTypeParameter(p));
const contextualType = getContextualType(node, skipBindingPatterns ? ContextFlags.SkipBindingPatterns : ContextFlags.None);
if (contextualType) {
const inferenceTargetType = getReturnTypeOfSignature(signature);
if (couldContainTypeVariables(inferenceTargetType)) {
// 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
// inferred for any outer call expression so far.
const outerContext = getInferenceContext(node);
const outerMapper = getMapperFromContext(cloneInferenceContext(outerContext, InferenceFlags.NoDefault));
const instantiatedType = instantiateType(contextualType, outerMapper);
// If the contextual type is a generic function type with a single call signature, we
// instantiate the type with its own type parameters and type arguments. This ensures that
// the type parameters are not erased to type any during type inference such that they can
// be inferred as actual types from the contextual type. For example:
// declare function arrayMap<T, U>(f: (x: T) => U): (a: T[]) => U[];
// const boxElements: <A>(a: A[]) => { value: A }[] = arrayMap(value => ({ value }));
// Above, the type of the 'value' parameter is inferred to be 'A'.
const contextualSignature = getSingleCallSignature(instantiatedType);
const inferenceSourceType = contextualSignature && contextualSignature.typeParameters ?
getOrCreateTypeFromSignature(getSignatureInstantiationWithoutFillingInTypeArguments(contextualSignature, contextualSignature.typeParameters)) :
instantiatedType;
// Inferences made from return types have lower priority than all other inferences.
inferTypes(context.inferences, inferenceSourceType, inferenceTargetType, InferencePriority.ReturnType);
const isFromBindingPattern = !skipBindingPatterns && getContextualType(node, ContextFlags.SkipBindingPatterns) !== contextualType;
// A return type inference from a binding pattern can be used in instantiating the contextual
// type of an argument later in inference, but cannot stand on its own as the final return type.
// It is incorporated into `context.returnMapper` which is used in `instantiateContextualType`,
// but doesn't need to go into `context.inferences`. This allows a an array binding pattern to
// produce a tuple for `T` in
// declare function f<T>(cb: () => T): T;
// const [e1, e2, e3] = f(() => [1, "hi", true]);
// but does not produce any inference for `T` in
// declare function f<T>(): T;
// const [e1, e2, e3] = f();
if (!isFromBindingPattern) {
// 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
// inferred for any outer call expression so far.
const outerMapper = getMapperFromContext(cloneInferenceContext(outerContext, InferenceFlags.NoDefault));
const instantiatedType = instantiateType(contextualType, outerMapper);
// If the contextual type is a generic function type with a single call signature, we
// instantiate the type with its own type parameters and type arguments. This ensures that
// the type parameters are not erased to type any during type inference such that they can
// be inferred as actual types from the contextual type. For example:
// declare function arrayMap<T, U>(f: (x: T) => U): (a: T[]) => U[];
// const boxElements: <A>(a: A[]) => { value: A }[] = arrayMap(value => ({ value }));
// Above, the type of the 'value' parameter is inferred to be 'A'.
const contextualSignature = getSingleCallSignature(instantiatedType);
const inferenceSourceType = contextualSignature && contextualSignature.typeParameters ?
getOrCreateTypeFromSignature(getSignatureInstantiationWithoutFillingInTypeArguments(contextualSignature, contextualSignature.typeParameters)) :
instantiatedType;
// Inferences made from return types have lower priority than all other inferences.
inferTypes(context.inferences, inferenceSourceType, inferenceTargetType, InferencePriority.ReturnType);
}
// Create a type mapper for instantiating generic contextual types using the inferences made
// from the return type. We need a separate inference pass here because (a) instantiation of
// the source type uses the outer context's return mapper (which excludes inferences made from

View File

@ -5874,7 +5874,7 @@ namespace ts {
ReturnType = 1 << 7, // Inference made from return type of generic function
LiteralKeyof = 1 << 8, // Inference made from a string literal to a keyof T
NoConstraints = 1 << 9, // Don't infer from constraints of instantiable types
AlwaysStrict = 1 << 10, // Always use strict rules for contravariant inferences
AlwaysStrict = 1 << 10, // Always use strict rules for contravariant inferences
MaxValue = 1 << 11, // Seed for inference priority tracking
PriorityImpliesCombination = ReturnType | MappedTypeConstraint | LiteralKeyof, // These priorities imply that the resulting type should be a combination of all candidates

View File

@ -0,0 +1,44 @@
tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts(2,7): error TS2571: Object is of type 'unknown'.
tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts(3,9): error TS2339: Property 'p1' does not exist on type 'unknown'.
tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts(4,7): error TS2461: Type 'unknown' is not an array type.
tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts(4,7): error TS2571: Object is of type 'unknown'.
tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts(5,7): error TS2461: Type 'unknown' is not an array type.
==== tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts (5 errors) ====
declare function f<T>(): T;
const {} = f(); // error (only in strictNullChecks)
~~
!!! error TS2571: Object is of type 'unknown'.
const { p1 } = f(); // error
~~
!!! error TS2339: Property 'p1' does not exist on type 'unknown'.
const [] = f(); // error
~~
!!! error TS2461: Type 'unknown' is not an array type.
~~
!!! error TS2571: Object is of type 'unknown'.
const [e1, e2] = f(); // error
~~~~~~~~
!!! error TS2461: Type 'unknown' is not an array type.
// Repro from #43605
type Dispatch<A = { type: any; [extraProps: string]: any }> = { <T extends A>(action: T): T };
type IFuncs = { readonly [key: string]: (...p: any) => void };
type IDestructuring<T extends IFuncs> = { readonly [key in keyof T]?: (...p: Parameters<T[key]>) => void };
type Destructuring<T extends IFuncs, U extends IDestructuring<T>> = (dispatch: Dispatch<any>, funcs: T) => U;
const funcs1 = {
funcA: (a: boolean): void => {},
funcB: (b: string, bb: string): void => {},
funcC: (c: number, cc: number, ccc: boolean): void => {},
};
type TFuncs1 = typeof funcs1;
declare function useReduxDispatch1<T extends IDestructuring<TFuncs1>>(destructuring: Destructuring<TFuncs1, T>): T;
const {} = 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)),
})
);

View File

@ -0,0 +1,61 @@
//// [bindingPatternCannotBeOnlyInferenceSource.ts]
declare function f<T>(): T;
const {} = f(); // error (only in strictNullChecks)
const { p1 } = f(); // error
const [] = f(); // error
const [e1, e2] = f(); // error
// Repro from #43605
type Dispatch<A = { type: any; [extraProps: string]: any }> = { <T extends A>(action: T): T };
type IFuncs = { readonly [key: string]: (...p: any) => void };
type IDestructuring<T extends IFuncs> = { readonly [key in keyof T]?: (...p: Parameters<T[key]>) => void };
type Destructuring<T extends IFuncs, U extends IDestructuring<T>> = (dispatch: Dispatch<any>, funcs: T) => U;
const funcs1 = {
funcA: (a: boolean): void => {},
funcB: (b: string, bb: string): void => {},
funcC: (c: number, cc: number, ccc: boolean): void => {},
};
type TFuncs1 = typeof funcs1;
declare function useReduxDispatch1<T extends IDestructuring<TFuncs1>>(destructuring: Destructuring<TFuncs1, T>): T;
const {} = 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)),
})
);
//// [bindingPatternCannotBeOnlyInferenceSource.js]
var _a = f(); // error (only in strictNullChecks)
var p1 = f().p1; // error
var _b = f(); // error
var _c = f(), e1 = _c[0], e2 = _c[1]; // error
var funcs1 = {
funcA: function (a) { },
funcB: function (b, bb) { },
funcC: function (c, cc, ccc) { }
};
var _d = useReduxDispatch1(function (d, f) { return ({
funcA: function () {
var p = [];
for (var _i = 0; _i < arguments.length; _i++) {
p[_i] = arguments[_i];
}
return d(f.funcA.apply(f, p));
},
funcB: function () {
var p = [];
for (var _i = 0; _i < arguments.length; _i++) {
p[_i] = arguments[_i];
}
return d(f.funcB.apply(f, p));
},
funcC: function () {
var p = [];
for (var _i = 0; _i < arguments.length; _i++) {
p[_i] = arguments[_i];
}
return d(f.funcC.apply(f, p));
}
}); });

View File

@ -0,0 +1,133 @@
=== tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts ===
declare function f<T>(): T;
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 0, 0))
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 0, 19))
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 0, 19))
const {} = f(); // error (only in strictNullChecks)
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 0, 0))
const { p1 } = f(); // error
>p1 : Symbol(p1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 2, 7))
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 0, 0))
const [] = f(); // error
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 0, 0))
const [e1, e2] = f(); // error
>e1 : Symbol(e1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 4, 7))
>e2 : Symbol(e2, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 4, 10))
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 0, 0))
// Repro from #43605
type Dispatch<A = { type: any; [extraProps: string]: any }> = { <T extends A>(action: T): T };
>Dispatch : Symbol(Dispatch, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 4, 21))
>A : Symbol(A, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 14))
>type : Symbol(type, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 19))
>extraProps : Symbol(extraProps, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 32))
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 65))
>A : Symbol(A, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 14))
>action : Symbol(action, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 78))
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 65))
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 65))
type IFuncs = { readonly [key: string]: (...p: any) => void };
>IFuncs : Symbol(IFuncs, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 94))
>key : Symbol(key, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 8, 26))
>p : Symbol(p, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 8, 41))
type IDestructuring<T extends IFuncs> = { readonly [key in keyof T]?: (...p: Parameters<T[key]>) => void };
>IDestructuring : Symbol(IDestructuring, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 8, 62))
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 9, 20))
>IFuncs : Symbol(IFuncs, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 94))
>key : Symbol(key, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 9, 52))
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 9, 20))
>p : Symbol(p, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 9, 71))
>Parameters : Symbol(Parameters, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 9, 20))
>key : Symbol(key, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 9, 52))
type Destructuring<T extends IFuncs, U extends IDestructuring<T>> = (dispatch: Dispatch<any>, funcs: T) => U;
>Destructuring : Symbol(Destructuring, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 9, 107))
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 10, 19))
>IFuncs : Symbol(IFuncs, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 7, 94))
>U : Symbol(U, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 10, 36))
>IDestructuring : Symbol(IDestructuring, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 8, 62))
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 10, 19))
>dispatch : Symbol(dispatch, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 10, 69))
>Dispatch : Symbol(Dispatch, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 4, 21))
>funcs : Symbol(funcs, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 10, 93))
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 10, 19))
>U : Symbol(U, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 10, 36))
const funcs1 = {
>funcs1 : Symbol(funcs1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 11, 5))
funcA: (a: boolean): void => {},
>funcA : Symbol(funcA, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 11, 16))
>a : Symbol(a, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 12, 12))
funcB: (b: string, bb: string): void => {},
>funcB : Symbol(funcB, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 12, 36))
>b : Symbol(b, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 13, 12))
>bb : Symbol(bb, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 13, 22))
funcC: (c: number, cc: number, ccc: boolean): void => {},
>funcC : Symbol(funcC, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 13, 47))
>c : Symbol(c, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 14, 12))
>cc : Symbol(cc, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 14, 22))
>ccc : Symbol(ccc, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 14, 34))
};
type TFuncs1 = typeof funcs1;
>TFuncs1 : Symbol(TFuncs1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 15, 2))
>funcs1 : Symbol(funcs1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 11, 5))
declare function useReduxDispatch1<T extends IDestructuring<TFuncs1>>(destructuring: Destructuring<TFuncs1, T>): T;
>useReduxDispatch1 : Symbol(useReduxDispatch1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 16, 29))
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 17, 35))
>IDestructuring : Symbol(IDestructuring, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 8, 62))
>TFuncs1 : Symbol(TFuncs1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 15, 2))
>destructuring : Symbol(destructuring, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 17, 70))
>Destructuring : Symbol(Destructuring, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 9, 107))
>TFuncs1 : Symbol(TFuncs1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 15, 2))
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 17, 35))
>T : Symbol(T, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 17, 35))
const {} = useReduxDispatch1(
>useReduxDispatch1 : Symbol(useReduxDispatch1, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 16, 29))
(d, f) => ({
>d : Symbol(d, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 5))
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 7))
funcA: (...p) => d(f.funcA(...p)), // p should be inferrable
>funcA : Symbol(funcA, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 16))
>p : Symbol(p, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 20, 16))
>d : Symbol(d, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 5))
>f.funcA : Symbol(funcA, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 11, 16))
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 7))
>funcA : Symbol(funcA, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 11, 16))
>p : Symbol(p, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 20, 16))
funcB: (...p) => d(f.funcB(...p)),
>funcB : Symbol(funcB, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 20, 42))
>p : Symbol(p, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 21, 16))
>d : Symbol(d, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 5))
>f.funcB : Symbol(funcB, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 12, 36))
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 7))
>funcB : Symbol(funcB, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 12, 36))
>p : Symbol(p, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 21, 16))
funcC: (...p) => d(f.funcC(...p)),
>funcC : Symbol(funcC, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 21, 42))
>p : Symbol(p, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 22, 16))
>d : Symbol(d, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 5))
>f.funcC : Symbol(funcC, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 13, 47))
>f : Symbol(f, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 19, 7))
>funcC : Symbol(funcC, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 13, 47))
>p : Symbol(p, Decl(bindingPatternCannotBeOnlyInferenceSource.ts, 22, 16))
})
);

View File

@ -0,0 +1,128 @@
=== tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts ===
declare function f<T>(): T;
>f : <T>() => T
const {} = f(); // error (only in strictNullChecks)
>f() : unknown
>f : <T>() => T
const { p1 } = f(); // error
>p1 : any
>f() : unknown
>f : <T>() => T
const [] = f(); // error
>f() : unknown
>f : <T>() => T
const [e1, e2] = f(); // error
>e1 : any
>e2 : any
>f() : unknown
>f : <T>() => T
// Repro from #43605
type Dispatch<A = { type: any; [extraProps: string]: any }> = { <T extends A>(action: T): T };
>Dispatch : Dispatch<A>
>type : any
>extraProps : string
>action : T
type IFuncs = { readonly [key: string]: (...p: any) => void };
>IFuncs : { readonly [key: string]: (...p: any) => void; }
>key : string
>p : any
type IDestructuring<T extends IFuncs> = { readonly [key in keyof T]?: (...p: Parameters<T[key]>) => void };
>IDestructuring : IDestructuring<T>
>p : Parameters<T[key]>
type Destructuring<T extends IFuncs, U extends IDestructuring<T>> = (dispatch: Dispatch<any>, funcs: T) => U;
>Destructuring : Destructuring<T, U>
>dispatch : Dispatch<any>
>funcs : T
const funcs1 = {
>funcs1 : { 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 => {},} : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
funcA: (a: boolean): void => {},
>funcA : (a: boolean) => void
>(a: boolean): void => {} : (a: boolean) => void
>a : boolean
funcB: (b: string, bb: string): void => {},
>funcB : (b: string, bb: string) => void
>(b: string, bb: string): void => {} : (b: string, bb: string) => void
>b : string
>bb : string
funcC: (c: number, cc: number, ccc: boolean): void => {},
>funcC : (c: number, cc: number, ccc: boolean) => void
>(c: number, cc: number, ccc: boolean): void => {} : (c: number, cc: number, ccc: boolean) => void
>c : number
>cc : number
>ccc : boolean
};
type TFuncs1 = typeof funcs1;
>TFuncs1 : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
>funcs1 : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
declare function useReduxDispatch1<T extends IDestructuring<TFuncs1>>(destructuring: Destructuring<TFuncs1, T>): T;
>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
>destructuring : Destructuring<{ funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }, T>
const {} = useReduxDispatch1(
>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; }
>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
(d, f) => ({
>(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; }
>d : Dispatch<any>
>f : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
>({ 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; }
>{ 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; }
funcA: (...p) => d(f.funcA(...p)), // p should be inferrable
>funcA : (a: boolean) => void
>(...p) => d(f.funcA(...p)) : (a: boolean) => void
>p : [a: boolean]
>d(f.funcA(...p)) : void
>d : Dispatch<any>
>f.funcA(...p) : void
>f.funcA : (a: boolean) => void
>f : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
>funcA : (a: boolean) => void
>...p : boolean
>p : [a: boolean]
funcB: (...p) => d(f.funcB(...p)),
>funcB : (b: string, bb: string) => void
>(...p) => d(f.funcB(...p)) : (b: string, bb: string) => void
>p : [b: string, bb: string]
>d(f.funcB(...p)) : void
>d : Dispatch<any>
>f.funcB(...p) : void
>f.funcB : (b: string, bb: string) => void
>f : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
>funcB : (b: string, bb: string) => void
>...p : string
>p : [b: string, bb: string]
funcC: (...p) => d(f.funcC(...p)),
>funcC : (c: number, cc: number, ccc: boolean) => void
>(...p) => d(f.funcC(...p)) : (c: number, cc: number, ccc: boolean) => void
>p : [c: number, cc: number, ccc: boolean]
>d(f.funcC(...p)) : void
>d : Dispatch<any>
>f.funcC(...p) : void
>f.funcC : (c: number, cc: number, ccc: boolean) => void
>f : { funcA: (a: boolean) => void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }
>funcC : (c: number, cc: number, ccc: boolean) => void
>...p : number | boolean
>p : [c: number, cc: number, ccc: boolean]
})
);

View File

@ -0,0 +1,9 @@
//// [bindingPatternContextualTypeDoesNotCauseWidening.ts]
declare function pick<O, T extends keyof O>(keys: T[], obj?: O): Pick<O, T>;
const _ = pick(['b'], { a: 'a', b: 'b' }); // T: "b"
const { } = pick(['b'], { a: 'a', b: 'b' }); // T: "b" | "a" ??? (before fix)
//// [bindingPatternContextualTypeDoesNotCauseWidening.js]
var _ = pick(['b'], { a: 'a', b: 'b' }); // T: "b"
var _a = pick(['b'], { a: 'a', b: 'b' }); // T: "b" | "a" ??? (before fix)

View File

@ -0,0 +1,25 @@
=== tests/cases/compiler/bindingPatternContextualTypeDoesNotCauseWidening.ts ===
declare function pick<O, T extends keyof O>(keys: T[], obj?: O): Pick<O, T>;
>pick : Symbol(pick, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 0))
>O : Symbol(O, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 22))
>T : Symbol(T, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 24))
>O : Symbol(O, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 22))
>keys : Symbol(keys, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 44))
>T : Symbol(T, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 24))
>obj : Symbol(obj, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 54))
>O : Symbol(O, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 22))
>Pick : Symbol(Pick, Decl(lib.es5.d.ts, --, --))
>O : Symbol(O, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 22))
>T : Symbol(T, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 24))
const _ = pick(['b'], { a: 'a', b: 'b' }); // T: "b"
>_ : Symbol(_, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 1, 5))
>pick : Symbol(pick, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 0))
>a : Symbol(a, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 1, 26))
>b : Symbol(b, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 1, 34))
const { } = pick(['b'], { a: 'a', b: 'b' }); // T: "b" | "a" ??? (before fix)
>pick : Symbol(pick, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 0, 0))
>a : Symbol(a, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 2, 26))
>b : Symbol(b, Decl(bindingPatternContextualTypeDoesNotCauseWidening.ts, 2, 34))

View File

@ -0,0 +1,29 @@
=== tests/cases/compiler/bindingPatternContextualTypeDoesNotCauseWidening.ts ===
declare function pick<O, T extends keyof O>(keys: T[], obj?: O): Pick<O, T>;
>pick : <O, T extends keyof O>(keys: T[], obj?: O) => Pick<O, T>
>keys : T[]
>obj : O
const _ = pick(['b'], { a: 'a', b: 'b' }); // T: "b"
>_ : Pick<{ a: string; b: string; }, "b">
>pick(['b'], { a: 'a', b: 'b' }) : Pick<{ a: string; b: string; }, "b">
>pick : <O, T extends keyof O>(keys: T[], obj?: O) => Pick<O, T>
>['b'] : "b"[]
>'b' : "b"
>{ a: 'a', b: 'b' } : { a: string; b: string; }
>a : string
>'a' : "a"
>b : string
>'b' : "b"
const { } = pick(['b'], { a: 'a', b: 'b' }); // T: "b" | "a" ??? (before fix)
>pick(['b'], { a: 'a', b: 'b' }) : Pick<{ a: string; b: string; }, "b">
>pick : <O, T extends keyof O>(keys: T[], obj?: O) => Pick<O, T>
>['b'] : "b"[]
>'b' : "b"
>{ a: 'a', b: 'b' } : { a: string; b: string; }
>a : string
>'a' : "a"
>b : string
>'b' : "b"

View File

@ -1,17 +0,0 @@
tests/cases/compiler/declarationEmitDestructuring4.ts(9,22): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
==== tests/cases/compiler/declarationEmitDestructuring4.ts (1 errors) ====
// For an array binding pattern with empty elements,
// we will not make any modification and will emit
// the similar binding pattern users' have written
function baz([]) { }
function baz1([] = [1,2,3]) { }
function baz2([[]] = [[1,2,3]]) { }
function baz3({}) { }
function baz4({} = { x: 10 }) { }
~
!!! error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.

View File

@ -1,17 +1,11 @@
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(1,13): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(1,19): error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
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; }'.
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; }'.
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; }'.
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; }'.
==== tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts (6 errors) ====
==== tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts (4 errors) ====
var { } = { x: 5, y: "hello" };
~
!!! error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
~
!!! error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
var { x4 } = { x4: 5, y4: "hello" };
~~
!!! error TS2353: Object literal may only specify known properties, and 'y4' does not exist in type '{ x4: any; }'.

View File

@ -1,17 +1,11 @@
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern1.ts(1,13): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern1.ts(1,19): error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
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; }'.
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; }'.
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; }'.
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; }'.
==== tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern1.ts (6 errors) ====
==== tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern1.ts (4 errors) ====
var { } = { x: 5, y: "hello" };
~
!!! error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
~
!!! error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
var { x4 } = { x4: 5, y4: "hello" };
~~
!!! error TS2353: Object literal may only specify known properties, and 'y4' does not exist in type '{ x4: any; }'.

View File

@ -1,6 +1,4 @@
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(5,16): error TS2493: Tuple type '[number, string]' of length '2' has no element at index '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 '{}'.
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 '{}'.
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; }'.
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; }'.
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):
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9): error TS2322: Type 'number' is not assignable to type 'string'.
==== tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts (22 errors) ====
==== tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts (20 errors) ====
function f0() {
var [] = [1, "hello"];
var [x] = [1, "hello"];
@ -46,11 +44,7 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9):
}
function f2() {
var { } = { x: 5, y: "hello" }; // Error, no x and y in target
~
!!! error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
~
!!! error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
var { } = { x: 5, y: "hello" }; // Ok, empty binding pattern means nothing
var { x } = { x: 5, y: "hello" }; // Error, no y in target
~
!!! error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{ x: any; }'.

View File

@ -20,7 +20,7 @@ function f1() {
}
function f2() {
var { } = { x: 5, y: "hello" }; // Error, no x and y in target
var { } = { x: 5, y: "hello" }; // Ok, empty binding pattern means nothing
var { x } = { x: 5, y: "hello" }; // Error, no y in target
var { y } = { x: 5, y: "hello" }; // Error, no x in target
var { x, y } = { x: 5, y: "hello" };
@ -206,7 +206,7 @@ function f1() {
var z;
}
function f2() {
var _a = { x: 5, y: "hello" }; // Error, no x and y in target
var _a = { x: 5, y: "hello" }; // Ok, empty binding pattern means nothing
var x = { x: 5, y: "hello" }.x; // Error, no y in target
var y = { x: 5, y: "hello" }.y; // Error, no x in target
var _b = { x: 5, y: "hello" }, x = _b.x, y = _b.y;

View File

@ -59,7 +59,7 @@ function f1() {
function f2() {
>f2 : Symbol(f2, Decl(declarationsAndAssignments.ts, 18, 1))
var { } = { x: 5, y: "hello" }; // Error, no x and y in target
var { } = { x: 5, y: "hello" }; // Ok, empty binding pattern means nothing
>x : Symbol(x, Decl(declarationsAndAssignments.ts, 21, 15))
>y : Symbol(y, Decl(declarationsAndAssignments.ts, 21, 21))

View File

@ -81,7 +81,7 @@ function f1() {
function f2() {
>f2 : () => void
var { } = { x: 5, y: "hello" }; // Error, no x and y in target
var { } = { x: 5, y: "hello" }; // Ok, empty binding pattern means nothing
>{ x: 5, y: "hello" } : { x: number; y: string; }
>x : number
>5 : 5

View File

@ -1,15 +0,0 @@
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 '{}'.
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 '{}'.
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 '{}'.
==== tests/cases/conformance/es6/destructuring/emptyObjectBindingPatternParameter04.ts (3 errors) ====
function f({} = {a: 1, b: "2", c: true}) {
~
!!! error TS2353: Object literal may only specify known properties, and 'a' does not exist in type '{}'.
~
!!! error TS2353: Object literal may only specify known properties, and 'b' does not exist in type '{}'.
~
!!! error TS2353: Object literal may only specify known properties, and 'c' does not exist in type '{}'.
var x, y, z;
}

View File

@ -0,0 +1,7 @@
//// [inferTupleFromBindingPattern.ts]
declare function f<T>(cb: () => T): T;
const [e1, e2, e3] = f(() => [1, "hi", true]);
//// [inferTupleFromBindingPattern.js]
var _a = f(function () { return [1, "hi", true]; }), e1 = _a[0], e2 = _a[1], e3 = _a[2];

View File

@ -0,0 +1,14 @@
=== tests/cases/compiler/inferTupleFromBindingPattern.ts ===
declare function f<T>(cb: () => T): T;
>f : Symbol(f, Decl(inferTupleFromBindingPattern.ts, 0, 0))
>T : Symbol(T, Decl(inferTupleFromBindingPattern.ts, 0, 19))
>cb : Symbol(cb, Decl(inferTupleFromBindingPattern.ts, 0, 22))
>T : Symbol(T, Decl(inferTupleFromBindingPattern.ts, 0, 19))
>T : Symbol(T, Decl(inferTupleFromBindingPattern.ts, 0, 19))
const [e1, e2, e3] = f(() => [1, "hi", true]);
>e1 : Symbol(e1, Decl(inferTupleFromBindingPattern.ts, 1, 7))
>e2 : Symbol(e2, Decl(inferTupleFromBindingPattern.ts, 1, 10))
>e3 : Symbol(e3, Decl(inferTupleFromBindingPattern.ts, 1, 14))
>f : Symbol(f, Decl(inferTupleFromBindingPattern.ts, 0, 0))

View File

@ -0,0 +1,17 @@
=== tests/cases/compiler/inferTupleFromBindingPattern.ts ===
declare function f<T>(cb: () => T): T;
>f : <T>(cb: () => T) => T
>cb : () => T
const [e1, e2, e3] = f(() => [1, "hi", true]);
>e1 : number
>e2 : string
>e3 : 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

View File

@ -10,8 +10,6 @@ tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts(12,8): e
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.
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.
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.
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 '{}'.
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 '{}'.
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; }'.
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; }'.
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):
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; }'.
==== tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts (20 errors) ====
==== tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts (18 errors) ====
// Missing properties
function f1() {
var { x, y } = {};
@ -69,10 +67,6 @@ tests/cases/conformance/es6/destructuring/missingAndExcessProperties.ts(31,16):
// Excess properties
function f3() {
var { } = { x: 0, y: 0 };
~
!!! error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
~
!!! error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
var { x } = { x: 0, y: 0 };
~
!!! error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{ x: any; }'.

View File

@ -0,0 +1,7 @@
//// [objectBindingPatternContextuallyTypesArgument.ts]
declare function id<T>(x: T): T;
const { f = (x: string) => x.length } = id({ f: x => x.charAt });
//// [objectBindingPatternContextuallyTypesArgument.js]
var _a = id({ f: function (x) { return x.charAt; } }).f, f = _a === void 0 ? function (x) { return x.length; } : _a;

View File

@ -0,0 +1,21 @@
=== tests/cases/compiler/objectBindingPatternContextuallyTypesArgument.ts ===
declare function id<T>(x: T): T;
>id : Symbol(id, Decl(objectBindingPatternContextuallyTypesArgument.ts, 0, 0))
>T : Symbol(T, Decl(objectBindingPatternContextuallyTypesArgument.ts, 0, 20))
>x : Symbol(x, Decl(objectBindingPatternContextuallyTypesArgument.ts, 0, 23))
>T : Symbol(T, Decl(objectBindingPatternContextuallyTypesArgument.ts, 0, 20))
>T : Symbol(T, Decl(objectBindingPatternContextuallyTypesArgument.ts, 0, 20))
const { f = (x: string) => x.length } = id({ f: x => x.charAt });
>f : Symbol(f, Decl(objectBindingPatternContextuallyTypesArgument.ts, 1, 7))
>x : Symbol(x, Decl(objectBindingPatternContextuallyTypesArgument.ts, 1, 13))
>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(objectBindingPatternContextuallyTypesArgument.ts, 1, 13))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>id : Symbol(id, Decl(objectBindingPatternContextuallyTypesArgument.ts, 0, 0))
>f : Symbol(f, Decl(objectBindingPatternContextuallyTypesArgument.ts, 1, 44))
>x : Symbol(x, Decl(objectBindingPatternContextuallyTypesArgument.ts, 1, 47))
>x.charAt : Symbol(String.charAt, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(objectBindingPatternContextuallyTypesArgument.ts, 1, 47))
>charAt : Symbol(String.charAt, Decl(lib.es5.d.ts, --, --))

View File

@ -0,0 +1,22 @@
=== tests/cases/compiler/objectBindingPatternContextuallyTypesArgument.ts ===
declare function id<T>(x: T): T;
>id : <T>(x: T) => T
>x : T
const { f = (x: string) => x.length } = id({ f: x => x.charAt });
>f : ((x: string) => number) | ((x: string) => (pos: number) => string)
>(x: string) => x.length : (x: string) => number
>x : string
>x.length : number
>x : string
>length : number
>id({ f: x => x.charAt }) : { f: (x: string) => (pos: number) => string; }
>id : <T>(x: T) => T
>{ f: x => x.charAt } : { f: (x: string) => (pos: number) => string; }
>f : (x: string) => (pos: number) => string
>x => x.charAt : (x: string) => (pos: number) => string
>x : string
>x.charAt : (pos: number) => string
>x : string
>charAt : (pos: number) => string

View File

@ -0,0 +1,27 @@
// @strictNullChecks: true
declare function f<T>(): T;
const {} = f(); // error (only in strictNullChecks)
const { p1 } = f(); // error
const [] = f(); // error
const [e1, e2] = f(); // error
// Repro from #43605
type Dispatch<A = { type: any; [extraProps: string]: any }> = { <T extends A>(action: T): T };
type IFuncs = { readonly [key: string]: (...p: any) => void };
type IDestructuring<T extends IFuncs> = { readonly [key in keyof T]?: (...p: Parameters<T[key]>) => void };
type Destructuring<T extends IFuncs, U extends IDestructuring<T>> = (dispatch: Dispatch<any>, funcs: T) => U;
const funcs1 = {
funcA: (a: boolean): void => {},
funcB: (b: string, bb: string): void => {},
funcC: (c: number, cc: number, ccc: boolean): void => {},
};
type TFuncs1 = typeof funcs1;
declare function useReduxDispatch1<T extends IDestructuring<TFuncs1>>(destructuring: Destructuring<TFuncs1, T>): T;
const {} = 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)),
})
);

View File

@ -0,0 +1,3 @@
declare function pick<O, T extends keyof O>(keys: T[], obj?: O): Pick<O, T>;
const _ = pick(['b'], { a: 'a', b: 'b' }); // T: "b"
const { } = pick(['b'], { a: 'a', b: 'b' }); // T: "b" | "a" ??? (before fix)

View File

@ -0,0 +1,2 @@
declare function f<T>(cb: () => T): T;
const [e1, e2, e3] = f(() => [1, "hi", true]);

View File

@ -0,0 +1,2 @@
declare function id<T>(x: T): T;
const { f = (x: string) => x.length } = id({ f: x => x.charAt });

View File

@ -19,7 +19,7 @@ function f1() {
}
function f2() {
var { } = { x: 5, y: "hello" }; // Error, no x and y in target
var { } = { x: 5, y: "hello" }; // Ok, empty binding pattern means nothing
var { x } = { x: 5, y: "hello" }; // Error, no y in target
var { y } = { x: 5, y: "hello" }; // Error, no x in target
var { x, y } = { x: 5, y: "hello" };