diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 025eeecc30624..b3b48e0211ae1 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -26517,7 +26517,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); } } @@ -27051,22 +27051,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; @@ -29902,29 +29900,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(f: (x: T) => U): (a: T[]) => U[]; - // const boxElements: (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(cb: () => T): T; + // const [e1, e2, e3] = f(() => [1, "hi", true]); + // but does not produce any inference for `T` in + // declare function f(): 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(f: (x: T) => U): (a: T[]) => U[]; + // const boxElements: (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 diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 717f802917822..f72207f24dce9 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5867,7 +5867,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 diff --git a/tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.errors.txt b/tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.errors.txt new file mode 100644 index 0000000000000..200c56f7689a1 --- /dev/null +++ b/tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.errors.txt @@ -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; + 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 = { (action: T): T }; + type IFuncs = { readonly [key: string]: (...p: any) => void }; + type IDestructuring = { readonly [key in keyof T]?: (...p: Parameters) => void }; + type Destructuring> = (dispatch: Dispatch, 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>(destructuring: Destructuring): 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)), + }) + ); + \ No newline at end of file diff --git a/tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.js b/tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.js new file mode 100644 index 0000000000000..4f8e285db06f8 --- /dev/null +++ b/tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.js @@ -0,0 +1,61 @@ +//// [bindingPatternCannotBeOnlyInferenceSource.ts] +declare function f(): T; +const {} = f(); // error (only in strictNullChecks) +const { p1 } = f(); // error +const [] = f(); // error +const [e1, e2] = f(); // error + +// Repro from #43605 +type Dispatch = { (action: T): T }; +type IFuncs = { readonly [key: string]: (...p: any) => void }; +type IDestructuring = { readonly [key in keyof T]?: (...p: Parameters) => void }; +type Destructuring> = (dispatch: Dispatch, 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>(destructuring: Destructuring): 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)); + } +}); }); diff --git a/tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.symbols b/tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.symbols new file mode 100644 index 0000000000000..031d39e3abd8b --- /dev/null +++ b/tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.symbols @@ -0,0 +1,133 @@ +=== tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts === +declare function f(): 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 = { (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 = { readonly [key in keyof T]?: (...p: Parameters) => 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> = (dispatch: Dispatch, 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>(destructuring: Destructuring): 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)) + + }) +); + diff --git a/tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.types b/tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.types new file mode 100644 index 0000000000000..1c81cf4f8db5b --- /dev/null +++ b/tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.types @@ -0,0 +1,128 @@ +=== tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts === +declare function f(): T; +>f : () => T + +const {} = f(); // error (only in strictNullChecks) +>f() : unknown +>f : () => T + +const { p1 } = f(); // error +>p1 : any +>f() : unknown +>f : () => T + +const [] = f(); // error +>f() : unknown +>f : () => T + +const [e1, e2] = f(); // error +>e1 : any +>e2 : any +>f() : unknown +>f : () => T + +// Repro from #43605 +type Dispatch = { (action: T): T }; +>Dispatch : Dispatch +>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 = { readonly [key in keyof T]?: (...p: Parameters) => void }; +>IDestructuring : IDestructuring +>p : Parameters + +type Destructuring> = (dispatch: Dispatch, funcs: T) => U; +>Destructuring : Destructuring +>dispatch : Dispatch +>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>(destructuring: Destructuring): T; +>useReduxDispatch1 : void; funcB: (b: string, bb: string) => void; funcC: (c: number, cc: number, ccc: boolean) => void; }>>(destructuring: Destructuring) => 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 : 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, 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 +>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 +>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 +>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 +>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] + + }) +); + diff --git a/tests/baselines/reference/bindingPatternContextualTypeDoesNotCauseWidening.js b/tests/baselines/reference/bindingPatternContextualTypeDoesNotCauseWidening.js new file mode 100644 index 0000000000000..fa74aadf41e43 --- /dev/null +++ b/tests/baselines/reference/bindingPatternContextualTypeDoesNotCauseWidening.js @@ -0,0 +1,9 @@ +//// [bindingPatternContextualTypeDoesNotCauseWidening.ts] +declare function pick(keys: T[], obj?: O): Pick; +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) diff --git a/tests/baselines/reference/bindingPatternContextualTypeDoesNotCauseWidening.symbols b/tests/baselines/reference/bindingPatternContextualTypeDoesNotCauseWidening.symbols new file mode 100644 index 0000000000000..bce99caf157b4 --- /dev/null +++ b/tests/baselines/reference/bindingPatternContextualTypeDoesNotCauseWidening.symbols @@ -0,0 +1,25 @@ +=== tests/cases/compiler/bindingPatternContextualTypeDoesNotCauseWidening.ts === +declare function pick(keys: T[], obj?: O): Pick; +>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)) + diff --git a/tests/baselines/reference/bindingPatternContextualTypeDoesNotCauseWidening.types b/tests/baselines/reference/bindingPatternContextualTypeDoesNotCauseWidening.types new file mode 100644 index 0000000000000..2aa44d96f6a97 --- /dev/null +++ b/tests/baselines/reference/bindingPatternContextualTypeDoesNotCauseWidening.types @@ -0,0 +1,29 @@ +=== tests/cases/compiler/bindingPatternContextualTypeDoesNotCauseWidening.ts === +declare function pick(keys: T[], obj?: O): Pick; +>pick : (keys: T[], obj?: O) => Pick +>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 : (keys: T[], obj?: O) => Pick +>['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 : (keys: T[], obj?: O) => Pick +>['b'] : "b"[] +>'b' : "b" +>{ a: 'a', b: 'b' } : { a: string; b: string; } +>a : string +>'a' : "a" +>b : string +>'b' : "b" + diff --git a/tests/baselines/reference/declarationEmitDestructuring4.errors.txt b/tests/baselines/reference/declarationEmitDestructuring4.errors.txt deleted file mode 100644 index 2787d5e66749f..0000000000000 --- a/tests/baselines/reference/declarationEmitDestructuring4.errors.txt +++ /dev/null @@ -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 '{}'. - - \ No newline at end of file diff --git a/tests/baselines/reference/declarationEmitDestructuringObjectLiteralPattern.errors.txt b/tests/baselines/reference/declarationEmitDestructuringObjectLiteralPattern.errors.txt index 63b68a6fc6b7f..9d89a96cf3c2c 100644 --- a/tests/baselines/reference/declarationEmitDestructuringObjectLiteralPattern.errors.txt +++ b/tests/baselines/reference/declarationEmitDestructuringObjectLiteralPattern.errors.txt @@ -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; }'. diff --git a/tests/baselines/reference/declarationEmitDestructuringObjectLiteralPattern1.errors.txt b/tests/baselines/reference/declarationEmitDestructuringObjectLiteralPattern1.errors.txt index bb8ec829b35f2..1bee984285cd1 100644 --- a/tests/baselines/reference/declarationEmitDestructuringObjectLiteralPattern1.errors.txt +++ b/tests/baselines/reference/declarationEmitDestructuringObjectLiteralPattern1.errors.txt @@ -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; }'. diff --git a/tests/baselines/reference/declarationsAndAssignments.errors.txt b/tests/baselines/reference/declarationsAndAssignments.errors.txt index 3a0b00b570ecf..1349f109dc125 100644 --- a/tests/baselines/reference/declarationsAndAssignments.errors.txt +++ b/tests/baselines/reference/declarationsAndAssignments.errors.txt @@ -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; }'. diff --git a/tests/baselines/reference/declarationsAndAssignments.js b/tests/baselines/reference/declarationsAndAssignments.js index f092d0c1f928f..7f803eb8021b0 100644 --- a/tests/baselines/reference/declarationsAndAssignments.js +++ b/tests/baselines/reference/declarationsAndAssignments.js @@ -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; diff --git a/tests/baselines/reference/declarationsAndAssignments.symbols b/tests/baselines/reference/declarationsAndAssignments.symbols index e5971d121f105..ce37c9e231f39 100644 --- a/tests/baselines/reference/declarationsAndAssignments.symbols +++ b/tests/baselines/reference/declarationsAndAssignments.symbols @@ -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)) diff --git a/tests/baselines/reference/declarationsAndAssignments.types b/tests/baselines/reference/declarationsAndAssignments.types index 8faf550681e2a..a7b72a6809424 100644 --- a/tests/baselines/reference/declarationsAndAssignments.types +++ b/tests/baselines/reference/declarationsAndAssignments.types @@ -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 diff --git a/tests/baselines/reference/emptyObjectBindingPatternParameter04.errors.txt b/tests/baselines/reference/emptyObjectBindingPatternParameter04.errors.txt deleted file mode 100644 index 52b69127f79c2..0000000000000 --- a/tests/baselines/reference/emptyObjectBindingPatternParameter04.errors.txt +++ /dev/null @@ -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; - } \ No newline at end of file diff --git a/tests/baselines/reference/inferTupleFromBindingPattern.js b/tests/baselines/reference/inferTupleFromBindingPattern.js new file mode 100644 index 0000000000000..19dce48f837f6 --- /dev/null +++ b/tests/baselines/reference/inferTupleFromBindingPattern.js @@ -0,0 +1,7 @@ +//// [inferTupleFromBindingPattern.ts] +declare function f(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]; diff --git a/tests/baselines/reference/inferTupleFromBindingPattern.symbols b/tests/baselines/reference/inferTupleFromBindingPattern.symbols new file mode 100644 index 0000000000000..015e3f2fe5aad --- /dev/null +++ b/tests/baselines/reference/inferTupleFromBindingPattern.symbols @@ -0,0 +1,14 @@ +=== tests/cases/compiler/inferTupleFromBindingPattern.ts === +declare function f(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)) + diff --git a/tests/baselines/reference/inferTupleFromBindingPattern.types b/tests/baselines/reference/inferTupleFromBindingPattern.types new file mode 100644 index 0000000000000..72292a6be0427 --- /dev/null +++ b/tests/baselines/reference/inferTupleFromBindingPattern.types @@ -0,0 +1,17 @@ +=== tests/cases/compiler/inferTupleFromBindingPattern.ts === +declare function f(cb: () => T): T; +>f : (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 : (cb: () => T) => T +>() => [1, "hi", true] : () => [number, string, boolean] +>[1, "hi", true] : [number, string, true] +>1 : 1 +>"hi" : "hi" +>true : true + diff --git a/tests/baselines/reference/missingAndExcessProperties.errors.txt b/tests/baselines/reference/missingAndExcessProperties.errors.txt index 7972d770320e6..a6e949f3a8930 100644 --- a/tests/baselines/reference/missingAndExcessProperties.errors.txt +++ b/tests/baselines/reference/missingAndExcessProperties.errors.txt @@ -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; }'. diff --git a/tests/baselines/reference/objectBindingPatternContextuallyTypesArgument.js b/tests/baselines/reference/objectBindingPatternContextuallyTypesArgument.js new file mode 100644 index 0000000000000..6be0f4361db21 --- /dev/null +++ b/tests/baselines/reference/objectBindingPatternContextuallyTypesArgument.js @@ -0,0 +1,7 @@ +//// [objectBindingPatternContextuallyTypesArgument.ts] +declare function id(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; diff --git a/tests/baselines/reference/objectBindingPatternContextuallyTypesArgument.symbols b/tests/baselines/reference/objectBindingPatternContextuallyTypesArgument.symbols new file mode 100644 index 0000000000000..77e2e0ad7d4af --- /dev/null +++ b/tests/baselines/reference/objectBindingPatternContextuallyTypesArgument.symbols @@ -0,0 +1,21 @@ +=== tests/cases/compiler/objectBindingPatternContextuallyTypesArgument.ts === +declare function id(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, --, --)) + diff --git a/tests/baselines/reference/objectBindingPatternContextuallyTypesArgument.types b/tests/baselines/reference/objectBindingPatternContextuallyTypesArgument.types new file mode 100644 index 0000000000000..b5c4974a07989 --- /dev/null +++ b/tests/baselines/reference/objectBindingPatternContextuallyTypesArgument.types @@ -0,0 +1,22 @@ +=== tests/cases/compiler/objectBindingPatternContextuallyTypesArgument.ts === +declare function id(x: T): T; +>id : (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 : (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 + diff --git a/tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts b/tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts new file mode 100644 index 0000000000000..361ae8631f62d --- /dev/null +++ b/tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts @@ -0,0 +1,27 @@ +// @strictNullChecks: true + +declare function f(): T; +const {} = f(); // error (only in strictNullChecks) +const { p1 } = f(); // error +const [] = f(); // error +const [e1, e2] = f(); // error + +// Repro from #43605 +type Dispatch = { (action: T): T }; +type IFuncs = { readonly [key: string]: (...p: any) => void }; +type IDestructuring = { readonly [key in keyof T]?: (...p: Parameters) => void }; +type Destructuring> = (dispatch: Dispatch, 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>(destructuring: Destructuring): 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)), + }) +); diff --git a/tests/cases/compiler/bindingPatternContextualTypeDoesNotCauseWidening.ts b/tests/cases/compiler/bindingPatternContextualTypeDoesNotCauseWidening.ts new file mode 100644 index 0000000000000..a86b8918ebf54 --- /dev/null +++ b/tests/cases/compiler/bindingPatternContextualTypeDoesNotCauseWidening.ts @@ -0,0 +1,3 @@ +declare function pick(keys: T[], obj?: O): Pick; +const _ = pick(['b'], { a: 'a', b: 'b' }); // T: "b" +const { } = pick(['b'], { a: 'a', b: 'b' }); // T: "b" | "a" ??? (before fix) diff --git a/tests/cases/compiler/inferTupleFromBindingPattern.ts b/tests/cases/compiler/inferTupleFromBindingPattern.ts new file mode 100644 index 0000000000000..7c0c1dea98193 --- /dev/null +++ b/tests/cases/compiler/inferTupleFromBindingPattern.ts @@ -0,0 +1,2 @@ +declare function f(cb: () => T): T; +const [e1, e2, e3] = f(() => [1, "hi", true]); diff --git a/tests/cases/compiler/objectBindingPatternContextuallyTypesArgument.ts b/tests/cases/compiler/objectBindingPatternContextuallyTypesArgument.ts new file mode 100644 index 0000000000000..dc79200ae6e8d --- /dev/null +++ b/tests/cases/compiler/objectBindingPatternContextuallyTypesArgument.ts @@ -0,0 +1,2 @@ +declare function id(x: T): T; +const { f = (x: string) => x.length } = id({ f: x => x.charAt }); diff --git a/tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts b/tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts index 7ab33f99a1e84..9167239cefdfd 100644 --- a/tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts +++ b/tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts @@ -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" };