From dc325fe21c29a6bbe286797f43460b42c8b13fcb Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 11 May 2022 16:40:39 -0700 Subject: [PATCH 01/10] WIP --- src/compiler/checker.ts | 74 +++++++++++-------- src/compiler/types.ts | 9 ++- .../compiler/inferTupleFromBindingPattern.ts | 29 ++++++++ 3 files changed, 76 insertions(+), 36 deletions(-) create mode 100644 tests/cases/compiler/inferTupleFromBindingPattern.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 025eeecc30624..daa45f35d76d8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -22403,7 +22403,7 @@ namespace ts { inferFromTypes((source as IndexedAccessType).objectType, (target as IndexedAccessType).objectType); inferFromTypes((source as IndexedAccessType).indexType, (target as IndexedAccessType).indexType); } - else if (source.flags & TypeFlags.StringMapping && target.flags & TypeFlags.StringMapping) { + else if (source. flags & TypeFlags.StringMapping && target.flags & TypeFlags.StringMapping) { if ((source as StringMappingType).symbol === (target as StringMappingType).symbol) { inferFromTypes((source as StringMappingType).type, (target as StringMappingType).type); } @@ -22943,39 +22943,46 @@ namespace ts { if (!inference.inferredType) { let inferredType: Type | undefined; const signature = context.signature; - if (signature) { - const inferredCovariantType = inference.candidates ? getCovariantInference(inference, signature) : undefined; - if (inference.contraCandidates) { - // If we have both co- and contra-variant inferences, we prefer the contra-variant inference - // unless the co-variant inference is a subtype of some contra-variant inference and not 'never'. - inferredType = inferredCovariantType && !(inferredCovariantType.flags & TypeFlags.Never) && - some(inference.contraCandidates, t => isTypeSubtypeOf(inferredCovariantType, t)) ? - inferredCovariantType : getContravariantInference(inference); - } - else if (inferredCovariantType) { - inferredType = inferredCovariantType; - } - else if (context.flags & InferenceFlags.NoDefault) { - // We use silentNeverType as the wildcard that signals no inferences. - inferredType = silentNeverType; + if (!(inference.priority! & InferencePriority.BindingPattern)) { + // Binding pattern inferences provide a contextual type for other inferences, + // but cannot stand alone - they are expected to be overwritten by another + // inference with higher priority before fixing. This prevents highly suspicious + // patterns like `function f(): T; const { foo } = f()` from inferring T as + // `{ foo: any }`. + if (signature) { + const inferredCovariantType = inference.candidates ? getCovariantInference(inference, signature) : undefined; + if (inference.contraCandidates) { + // If we have both co- and contra-variant inferences, we prefer the contra-variant inference + // unless the co-variant inference is a subtype of some contra-variant inference and not 'never'. + inferredType = inferredCovariantType && !(inferredCovariantType.flags & TypeFlags.Never) && + some(inference.contraCandidates, t => isTypeSubtypeOf(inferredCovariantType, t)) ? + inferredCovariantType : getContravariantInference(inference); + } + else if (inferredCovariantType) { + inferredType = inferredCovariantType; + } + else if (context.flags & InferenceFlags.NoDefault) { + // We use silentNeverType as the wildcard that signals no inferences. + inferredType = silentNeverType; + } + else { + // Infer either the default or the empty object type when no inferences were + // made. It is important to remember that in this case, inference still + // succeeds, meaning there is no error for not having inference candidates. An + // inference error only occurs when there are *conflicting* candidates, i.e. + // candidates with no common supertype. + const defaultType = getDefaultFromTypeParameter(inference.typeParameter); + if (defaultType) { + // Instantiate the default type. Any forward reference to a type + // parameter should be instantiated to the empty object type. + inferredType = instantiateType(defaultType, mergeTypeMappers(createBackreferenceMapper(context, index), context.nonFixingMapper)); + } + } } else { - // Infer either the default or the empty object type when no inferences were - // made. It is important to remember that in this case, inference still - // succeeds, meaning there is no error for not having inference candidates. An - // inference error only occurs when there are *conflicting* candidates, i.e. - // candidates with no common supertype. - const defaultType = getDefaultFromTypeParameter(inference.typeParameter); - if (defaultType) { - // Instantiate the default type. Any forward reference to a type - // parameter should be instantiated to the empty object type. - inferredType = instantiateType(defaultType, mergeTypeMappers(createBackreferenceMapper(context, index), context.nonFixingMapper)); - } + inferredType = getTypeFromInference(inference); } } - else { - inferredType = getTypeFromInference(inference); - } inference.inferredType = inferredType || getDefaultTypeArgumentType(!!(context.flags & InferenceFlags.AnyDefault)); @@ -29902,7 +29909,8 @@ 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)) { @@ -29924,7 +29932,9 @@ namespace ts { 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; + const priority = InferencePriority.ReturnType | (isFromBindingPattern ? InferencePriority.BindingPattern : 0); + inferTypes(context.inferences, inferenceSourceType, inferenceTargetType, priority); // 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..b0f1f4c869efe 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5865,10 +5865,11 @@ namespace ts { MappedTypeConstraint = 1 << 5, // Reverse inference for mapped type ContravariantConditional = 1 << 6, // Conditional type in contravariant position 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 - MaxValue = 1 << 11, // Seed for inference priority tracking + BindingPattern = 1 << 8, // Inference made from binding pattern + LiteralKeyof = 1 << 9, // Inference made from a string literal to a keyof T + NoConstraints = 1 << 10, // Don't infer from constraints of instantiable types + AlwaysStrict = 1 << 11, // Always use strict rules for contravariant inferences + MaxValue = 1 << 12, // Seed for inference priority tracking PriorityImpliesCombination = ReturnType | MappedTypeConstraint | LiteralKeyof, // These priorities imply that the resulting type should be a combination of all candidates Circularity = -1, // Inference circularity (value less than all other priorities) diff --git a/tests/cases/compiler/inferTupleFromBindingPattern.ts b/tests/cases/compiler/inferTupleFromBindingPattern.ts new file mode 100644 index 0000000000000..c839d16f8e8e2 --- /dev/null +++ b/tests/cases/compiler/inferTupleFromBindingPattern.ts @@ -0,0 +1,29 @@ +declare function f2(cb: () => T): T; +const [e1, e2, e3] = f2(() => [1, "hi", true]); + +declare function f1(): T; +const {} = f1(); // error +const { p1 } = f1(); // error + +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" ??? + +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)), + funcB: (...p) => d(f.funcB(...p)), + funcC: (...p) => d(f.funcC(...p)) + }) +); From b00e6b51a228e15b740b0e24dcd1558fe1727c53 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Thu, 12 May 2022 09:39:19 -0700 Subject: [PATCH 02/10] =?UTF-8?q?Don=E2=80=99t=20widen=20literals=20based?= =?UTF-8?q?=20on=20bogus=20contextual=20type=20instantiation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/compiler/checker.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index daa45f35d76d8..350bff0aa8a13 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -21248,8 +21248,8 @@ namespace ts { type; } - function getWidenedLiteralLikeTypeForContextualType(type: Type, contextualType: Type | undefined) { - if (!isLiteralOfContextualType(type, contextualType)) { + function getWidenedLiteralLikeTypeForContextualType(type: Type, contextualType: Type | undefined, contextNode?: Node) { + if (!isLiteralOfContextualType(type, contextualType) && !(contextNode && isLiteralOfContextualType(type, instantiateContextualType(contextualType, contextNode)))) { type = getWidenedUniqueESSymbolType(getWidenedLiteralType(type)); } return type; @@ -34495,7 +34495,7 @@ namespace ts { const type = checkExpression(node, checkMode, forceTuple); return isConstContext(node) || isCommonJsExportedExpression(node) ? getRegularTypeOfLiteralType(type) : isTypeAssertion(node) ? type : - getWidenedLiteralLikeTypeForContextualType(type, instantiateContextualType(arguments.length === 2 ? getContextualType(node) : contextualType, node)); + getWidenedLiteralLikeTypeForContextualType(type, arguments.length === 2 ? getContextualType(node) : contextualType, node); } function checkPropertyAssignment(node: PropertyAssignment, checkMode?: CheckMode): Type { From 73e58ea929e3004b9b8e4a9eb0acef6caf2f88bb Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Thu, 12 May 2022 10:06:10 -0700 Subject: [PATCH 03/10] Split tests --- src/compiler/checker.ts | 12 ++++--- ...ndingPatternCannotBeOnlyInferenceSource.ts | 25 +++++++++++++++ ...tternContextualTypeDoesNotCauseWidening.ts | 3 ++ .../compiler/inferTupleFromBindingPattern.ts | 31 ++----------------- 4 files changed, 38 insertions(+), 33 deletions(-) create mode 100644 tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts create mode 100644 tests/cases/compiler/bindingPatternContextualTypeDoesNotCauseWidening.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 350bff0aa8a13..25fd500456710 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -21248,11 +21248,15 @@ namespace ts { type; } - function getWidenedLiteralLikeTypeForContextualType(type: Type, contextualType: Type | undefined, contextNode?: Node) { - if (!isLiteralOfContextualType(type, contextualType) && !(contextNode && isLiteralOfContextualType(type, instantiateContextualType(contextualType, contextNode)))) { - type = getWidenedUniqueESSymbolType(getWidenedLiteralType(type)); + function getWidenedLiteralLikeTypeForContextualType(type: Type, contextualType: Type | undefined, node?: Node) { + if (isLiteralOfContextualType(type, contextualType)) { + return type; } - return type; + const instantiatedContextualType = node && instantiateContextualType(contextualType, node) || contextualType; + if (instantiatedContextualType !== contextualType && isLiteralOfContextualType(type, instantiatedContextualType)) { + return type; + } + return getWidenedUniqueESSymbolType(getWidenedLiteralType(type)); } function getWidenedLiteralLikeTypeForContextualReturnTypeIfNeeded(type: Type | undefined, contextualSignatureReturnType: Type | undefined, isAsync: boolean) { diff --git a/tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts b/tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts new file mode 100644 index 0000000000000..0ac5a1f26b8cc --- /dev/null +++ b/tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts @@ -0,0 +1,25 @@ +declare function f(): T; +const {} = f(); // error +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 index c839d16f8e8e2..7c0c1dea98193 100644 --- a/tests/cases/compiler/inferTupleFromBindingPattern.ts +++ b/tests/cases/compiler/inferTupleFromBindingPattern.ts @@ -1,29 +1,2 @@ -declare function f2(cb: () => T): T; -const [e1, e2, e3] = f2(() => [1, "hi", true]); - -declare function f1(): T; -const {} = f1(); // error -const { p1 } = f1(); // error - -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" ??? - -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)), - funcB: (...p) => d(f.funcB(...p)), - funcC: (...p) => d(f.funcC(...p)) - }) -); +declare function f(cb: () => T): T; +const [e1, e2, e3] = f(() => [1, "hi", true]); From 65f453f98b06f57034fd196650ae0d13dce09e58 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Thu, 12 May 2022 11:12:44 -0700 Subject: [PATCH 04/10] Skip unnecessary inference pass --- src/compiler/checker.ts | 134 +++++++++--------- src/compiler/types.ts | 9 +- ...ndingPatternCannotBeOnlyInferenceSource.ts | 4 +- 3 files changed, 76 insertions(+), 71 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 25fd500456710..7d15bc9adc33c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -22947,46 +22947,39 @@ namespace ts { if (!inference.inferredType) { let inferredType: Type | undefined; const signature = context.signature; - if (!(inference.priority! & InferencePriority.BindingPattern)) { - // Binding pattern inferences provide a contextual type for other inferences, - // but cannot stand alone - they are expected to be overwritten by another - // inference with higher priority before fixing. This prevents highly suspicious - // patterns like `function f(): T; const { foo } = f()` from inferring T as - // `{ foo: any }`. - if (signature) { - const inferredCovariantType = inference.candidates ? getCovariantInference(inference, signature) : undefined; - if (inference.contraCandidates) { - // If we have both co- and contra-variant inferences, we prefer the contra-variant inference - // unless the co-variant inference is a subtype of some contra-variant inference and not 'never'. - inferredType = inferredCovariantType && !(inferredCovariantType.flags & TypeFlags.Never) && - some(inference.contraCandidates, t => isTypeSubtypeOf(inferredCovariantType, t)) ? - inferredCovariantType : getContravariantInference(inference); - } - else if (inferredCovariantType) { - inferredType = inferredCovariantType; - } - else if (context.flags & InferenceFlags.NoDefault) { - // We use silentNeverType as the wildcard that signals no inferences. - inferredType = silentNeverType; - } - else { - // Infer either the default or the empty object type when no inferences were - // made. It is important to remember that in this case, inference still - // succeeds, meaning there is no error for not having inference candidates. An - // inference error only occurs when there are *conflicting* candidates, i.e. - // candidates with no common supertype. - const defaultType = getDefaultFromTypeParameter(inference.typeParameter); - if (defaultType) { - // Instantiate the default type. Any forward reference to a type - // parameter should be instantiated to the empty object type. - inferredType = instantiateType(defaultType, mergeTypeMappers(createBackreferenceMapper(context, index), context.nonFixingMapper)); - } - } + if (signature) { + const inferredCovariantType = inference.candidates ? getCovariantInference(inference, signature) : undefined; + if (inference.contraCandidates) { + // If we have both co- and contra-variant inferences, we prefer the contra-variant inference + // unless the co-variant inference is a subtype of some contra-variant inference and not 'never'. + inferredType = inferredCovariantType && !(inferredCovariantType.flags & TypeFlags.Never) && + some(inference.contraCandidates, t => isTypeSubtypeOf(inferredCovariantType, t)) ? + inferredCovariantType : getContravariantInference(inference); + } + else if (inferredCovariantType) { + inferredType = inferredCovariantType; + } + else if (context.flags & InferenceFlags.NoDefault) { + // We use silentNeverType as the wildcard that signals no inferences. + inferredType = silentNeverType; } else { - inferredType = getTypeFromInference(inference); + // Infer either the default or the empty object type when no inferences were + // made. It is important to remember that in this case, inference still + // succeeds, meaning there is no error for not having inference candidates. An + // inference error only occurs when there are *conflicting* candidates, i.e. + // candidates with no common supertype. + const defaultType = getDefaultFromTypeParameter(inference.typeParameter); + if (defaultType) { + // Instantiate the default type. Any forward reference to a type + // parameter should be instantiated to the empty object type. + inferredType = instantiateType(defaultType, mergeTypeMappers(createBackreferenceMapper(context, index), context.nonFixingMapper)); + } } } + else { + inferredType = getTypeFromInference(inference); + } inference.inferredType = inferredType || getDefaultTypeArgumentType(!!(context.flags & InferenceFlags.AnyDefault)); @@ -27062,22 +27055,22 @@ 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 && (inferenceContext.returnMapper || 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.returnMapper + ? combineTypeMappers(inferenceContext.nonFixingMapper, inferenceContext.returnMapper) + : 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; @@ -29918,27 +29911,38 @@ namespace ts { 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. const isFromBindingPattern = !skipBindingPatterns && getContextualType(node, ContextFlags.SkipBindingPatterns) !== contextualType; - const priority = InferencePriority.ReturnType | (isFromBindingPattern ? InferencePriority.BindingPattern : 0); - inferTypes(context.inferences, inferenceSourceType, inferenceTargetType, priority); + // 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 b0f1f4c869efe..f72207f24dce9 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5865,11 +5865,10 @@ namespace ts { MappedTypeConstraint = 1 << 5, // Reverse inference for mapped type ContravariantConditional = 1 << 6, // Conditional type in contravariant position ReturnType = 1 << 7, // Inference made from return type of generic function - BindingPattern = 1 << 8, // Inference made from binding pattern - LiteralKeyof = 1 << 9, // Inference made from a string literal to a keyof T - NoConstraints = 1 << 10, // Don't infer from constraints of instantiable types - AlwaysStrict = 1 << 11, // Always use strict rules for contravariant inferences - MaxValue = 1 << 12, // Seed for inference priority tracking + 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 + 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 Circularity = -1, // Inference circularity (value less than all other priorities) diff --git a/tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts b/tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts index 0ac5a1f26b8cc..361ae8631f62d 100644 --- a/tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts +++ b/tests/cases/compiler/bindingPatternCannotBeOnlyInferenceSource.ts @@ -1,5 +1,7 @@ +// @strictNullChecks: true + declare function f(): T; -const {} = f(); // error +const {} = f(); // error (only in strictNullChecks) const { p1 } = f(); // error const [] = f(); // error const [e1, e2] = f(); // error From c923bafe15568f3ca04f6f5e02e046718ebff163 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Thu, 12 May 2022 11:17:41 -0700 Subject: [PATCH 05/10] Accept test baselines --- ...ternCannotBeOnlyInferenceSource.errors.txt | 44 ++++++ ...ndingPatternCannotBeOnlyInferenceSource.js | 61 ++++++++ ...PatternCannotBeOnlyInferenceSource.symbols | 133 ++++++++++++++++++ ...ngPatternCannotBeOnlyInferenceSource.types | 128 +++++++++++++++++ ...tternContextualTypeDoesNotCauseWidening.js | 9 ++ ...ContextualTypeDoesNotCauseWidening.symbols | 25 ++++ ...rnContextualTypeDoesNotCauseWidening.types | 29 ++++ .../reference/inferTupleFromBindingPattern.js | 7 + .../inferTupleFromBindingPattern.symbols | 14 ++ .../inferTupleFromBindingPattern.types | 17 +++ 10 files changed, 467 insertions(+) create mode 100644 tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.errors.txt create mode 100644 tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.js create mode 100644 tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.symbols create mode 100644 tests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.types create mode 100644 tests/baselines/reference/bindingPatternContextualTypeDoesNotCauseWidening.js create mode 100644 tests/baselines/reference/bindingPatternContextualTypeDoesNotCauseWidening.symbols create mode 100644 tests/baselines/reference/bindingPatternContextualTypeDoesNotCauseWidening.types create mode 100644 tests/baselines/reference/inferTupleFromBindingPattern.js create mode 100644 tests/baselines/reference/inferTupleFromBindingPattern.symbols create mode 100644 tests/baselines/reference/inferTupleFromBindingPattern.types 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/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 + From 7e8c56cbf13ab3dade7df45f58c137e558268f9c Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Thu, 12 May 2022 11:26:17 -0700 Subject: [PATCH 06/10] Fix stray edit --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7d15bc9adc33c..20613d966d945 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -22407,7 +22407,7 @@ namespace ts { inferFromTypes((source as IndexedAccessType).objectType, (target as IndexedAccessType).objectType); inferFromTypes((source as IndexedAccessType).indexType, (target as IndexedAccessType).indexType); } - else if (source. flags & TypeFlags.StringMapping && target.flags & TypeFlags.StringMapping) { + else if (source.flags & TypeFlags.StringMapping && target.flags & TypeFlags.StringMapping) { if ((source as StringMappingType).symbol === (target as StringMappingType).symbol) { inferFromTypes((source as StringMappingType).type, (target as StringMappingType).type); } From 7dc1952a8229c021c5bfd573f0c61930552c9613 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Fri, 13 May 2022 15:10:25 -0700 Subject: [PATCH 07/10] Fix type mapper combination --- src/compiler/checker.ts | 26 +++++++++++++++---- src/compiler/types.ts | 1 + ...BindingPatternContextuallyTypesArgument.js | 7 +++++ ...ngPatternContextuallyTypesArgument.symbols | 21 +++++++++++++++ ...dingPatternContextuallyTypesArgument.types | 22 ++++++++++++++++ ...BindingPatternContextuallyTypesArgument.ts | 2 ++ 6 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 tests/baselines/reference/objectBindingPatternContextuallyTypesArgument.js create mode 100644 tests/baselines/reference/objectBindingPatternContextuallyTypesArgument.symbols create mode 100644 tests/baselines/reference/objectBindingPatternContextuallyTypesArgument.types create mode 100644 tests/cases/compiler/objectBindingPatternContextuallyTypesArgument.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 20613d966d945..b3a590f3e9018 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -21840,6 +21840,20 @@ namespace ts { return t; } + function mapToInferredTypeIncludingReturnTypeInferences(context: InferenceContext, returnContext: InferenceContext, t: Type): Type { + const inferences = context.inferences; + for (let i = 0; i < inferences.length; i++) { + const inference = inferences[i]; + if (t === inference.typeParameter) { + if (inference.inferredType || hasInferenceCandidates(inference)) { + return getInferredType(context, i); + } + return getMappedType(t, returnContext.mapper); + } + } + return t; + } + function clearCachedInferences(inferences: InferenceInfo[]) { for (const inference of inferences) { if (!inference.isFixed) { @@ -27055,12 +27069,10 @@ 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 && contextFlags! & ContextFlags.Signature && (inferenceContext.returnMapper || some(inferenceContext.inferences, hasInferenceCandidates))) { + if (inferenceContext && contextFlags! & ContextFlags.Signature && (inferenceContext.combinedReturnMapper || 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. - return instantiateInstantiableTypes(contextualType, inferenceContext.returnMapper - ? combineTypeMappers(inferenceContext.nonFixingMapper, inferenceContext.returnMapper) - : inferenceContext.nonFixingMapper); + return instantiateInstantiableTypes(contextualType, inferenceContext.combinedReturnMapper || inferenceContext.nonFixingMapper); } if (inferenceContext?.returnMapper) { // For other purposes (e.g. determining whether to produce literal types) we only @@ -29950,7 +29962,11 @@ namespace ts { const returnContext = createInferenceContext(signature.typeParameters!, signature, context.flags); const returnSourceType = instantiateType(contextualType, outerContext && outerContext.returnMapper); inferTypes(returnContext.inferences, returnSourceType, inferenceTargetType); - context.returnMapper = some(returnContext.inferences, hasInferenceCandidates) ? getMapperFromContext(cloneInferredPartOfContext(returnContext)) : undefined; + if (some(returnContext.inferences, hasInferenceCandidates)) { + const clonedReturnContext = cloneInferredPartOfContext(returnContext)!; + context.returnMapper = getMapperFromContext(clonedReturnContext); + context.combinedReturnMapper = makeFunctionTypeMapper(t => mapToInferredTypeIncludingReturnTypeInferences(context, clonedReturnContext, t)); + } } } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index f72207f24dce9..fbe0d3c17a429 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5922,6 +5922,7 @@ namespace ts { mapper: TypeMapper; // Mapper that fixes inferences nonFixingMapper: TypeMapper; // Mapper that doesn't fix inferences returnMapper?: TypeMapper; // Type mapper for inferences from return types (if any) + combinedReturnMapper?: TypeMapper; // Non-fixing mapper combined with return mapper for contextual signature instantiation inferredTypeParameters?: readonly TypeParameter[]; // Inferred type parameters for function result intraExpressionInferenceSites?: IntraExpressionInferenceSite[]; } 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/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 }); From 96f2f52e1a79262e60e60fd30b0f51da2c14d8bf Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 18 May 2022 10:12:27 -0700 Subject: [PATCH 08/10] Revert src/ of 7dc1952a82 --- src/compiler/checker.ts | 26 +++++--------------------- src/compiler/types.ts | 1 - 2 files changed, 5 insertions(+), 22 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b3a590f3e9018..20613d966d945 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -21840,20 +21840,6 @@ namespace ts { return t; } - function mapToInferredTypeIncludingReturnTypeInferences(context: InferenceContext, returnContext: InferenceContext, t: Type): Type { - const inferences = context.inferences; - for (let i = 0; i < inferences.length; i++) { - const inference = inferences[i]; - if (t === inference.typeParameter) { - if (inference.inferredType || hasInferenceCandidates(inference)) { - return getInferredType(context, i); - } - return getMappedType(t, returnContext.mapper); - } - } - return t; - } - function clearCachedInferences(inferences: InferenceInfo[]) { for (const inference of inferences) { if (!inference.isFixed) { @@ -27069,10 +27055,12 @@ 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 && contextFlags! & ContextFlags.Signature && (inferenceContext.combinedReturnMapper || some(inferenceContext.inferences, hasInferenceCandidates))) { + if (inferenceContext && contextFlags! & ContextFlags.Signature && (inferenceContext.returnMapper || 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. - return instantiateInstantiableTypes(contextualType, inferenceContext.combinedReturnMapper || inferenceContext.nonFixingMapper); + return instantiateInstantiableTypes(contextualType, inferenceContext.returnMapper + ? combineTypeMappers(inferenceContext.nonFixingMapper, inferenceContext.returnMapper) + : inferenceContext.nonFixingMapper); } if (inferenceContext?.returnMapper) { // For other purposes (e.g. determining whether to produce literal types) we only @@ -29962,11 +29950,7 @@ namespace ts { const returnContext = createInferenceContext(signature.typeParameters!, signature, context.flags); const returnSourceType = instantiateType(contextualType, outerContext && outerContext.returnMapper); inferTypes(returnContext.inferences, returnSourceType, inferenceTargetType); - if (some(returnContext.inferences, hasInferenceCandidates)) { - const clonedReturnContext = cloneInferredPartOfContext(returnContext)!; - context.returnMapper = getMapperFromContext(clonedReturnContext); - context.combinedReturnMapper = makeFunctionTypeMapper(t => mapToInferredTypeIncludingReturnTypeInferences(context, clonedReturnContext, t)); - } + context.returnMapper = some(returnContext.inferences, hasInferenceCandidates) ? getMapperFromContext(cloneInferredPartOfContext(returnContext)) : undefined; } } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index fbe0d3c17a429..f72207f24dce9 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5922,7 +5922,6 @@ namespace ts { mapper: TypeMapper; // Mapper that fixes inferences nonFixingMapper: TypeMapper; // Mapper that doesn't fix inferences returnMapper?: TypeMapper; // Type mapper for inferences from return types (if any) - combinedReturnMapper?: TypeMapper; // Non-fixing mapper combined with return mapper for contextual signature instantiation inferredTypeParameters?: readonly TypeParameter[]; // Inferred type parameters for function result intraExpressionInferenceSites?: IntraExpressionInferenceSite[]; } From 024bf7ae2a052bfb7785f152ef5572e70ff6c611 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 18 May 2022 10:58:30 -0700 Subject: [PATCH 09/10] Make empty binding pattern provide no contextual type --- src/compiler/checker.ts | 22 +++++++------------ .../declarationEmitDestructuring4.errors.txt | 17 -------------- ...structuringObjectLiteralPattern.errors.txt | 8 +------ ...tructuringObjectLiteralPattern1.errors.txt | 8 +------ .../declarationsAndAssignments.errors.txt | 10 ++------- ...ObjectBindingPatternParameter04.errors.txt | 15 ------------- .../missingAndExcessProperties.errors.txt | 8 +------ .../declarationsAndAssignments.ts | 2 +- 8 files changed, 14 insertions(+), 76 deletions(-) delete mode 100644 tests/baselines/reference/declarationEmitDestructuring4.errors.txt delete mode 100644 tests/baselines/reference/emptyObjectBindingPatternParameter04.errors.txt diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 20613d966d945..b3b48e0211ae1 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -21248,15 +21248,11 @@ namespace ts { type; } - function getWidenedLiteralLikeTypeForContextualType(type: Type, contextualType: Type | undefined, node?: Node) { - if (isLiteralOfContextualType(type, contextualType)) { - return type; - } - const instantiatedContextualType = node && instantiateContextualType(contextualType, node) || contextualType; - if (instantiatedContextualType !== contextualType && isLiteralOfContextualType(type, instantiatedContextualType)) { - return type; + function getWidenedLiteralLikeTypeForContextualType(type: Type, contextualType: Type | undefined) { + if (!isLiteralOfContextualType(type, contextualType)) { + type = getWidenedUniqueESSymbolType(getWidenedLiteralType(type)); } - return getWidenedUniqueESSymbolType(getWidenedLiteralType(type)); + return type; } function getWidenedLiteralLikeTypeForContextualReturnTypeIfNeeded(type: Type | undefined, contextualSignatureReturnType: Type | undefined, isAsync: boolean) { @@ -26521,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); } } @@ -27055,12 +27051,10 @@ 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 && contextFlags! & ContextFlags.Signature && (inferenceContext.returnMapper || 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. - return instantiateInstantiableTypes(contextualType, inferenceContext.returnMapper - ? combineTypeMappers(inferenceContext.nonFixingMapper, inferenceContext.returnMapper) - : inferenceContext.nonFixingMapper); + return instantiateInstantiableTypes(contextualType, inferenceContext.nonFixingMapper); } if (inferenceContext?.returnMapper) { // For other purposes (e.g. determining whether to produce literal types) we only @@ -34503,7 +34497,7 @@ namespace ts { const type = checkExpression(node, checkMode, forceTuple); return isConstContext(node) || isCommonJsExportedExpression(node) ? getRegularTypeOfLiteralType(type) : isTypeAssertion(node) ? type : - getWidenedLiteralLikeTypeForContextualType(type, arguments.length === 2 ? getContextualType(node) : contextualType, node); + getWidenedLiteralLikeTypeForContextualType(type, instantiateContextualType(arguments.length === 2 ? getContextualType(node) : contextualType, node)); } function checkPropertyAssignment(node: PropertyAssignment, checkMode?: CheckMode): Type { 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/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/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/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" }; From 4537a794e7d76ce4a555281e64b9383ba5a7049d Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 18 May 2022 12:55:36 -0700 Subject: [PATCH 10/10] Add missed baseline --- tests/baselines/reference/declarationsAndAssignments.js | 4 ++-- tests/baselines/reference/declarationsAndAssignments.symbols | 2 +- tests/baselines/reference/declarationsAndAssignments.types | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) 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