diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cc64670dd993c..51c56a534802b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -24697,7 +24697,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { if (constraintType.flags & TypeFlags.TypeParameter) { // We're inferring from some source type S to a mapped type { [P in K]: X }, where K is a type // parameter. First infer from 'keyof S' to K. - inferWithPriority(getIndexType(source), constraintType, InferencePriority.MappedTypeConstraint); + inferWithPriority(getIndexType(source, /*indexFlags*/ !!source.pattern ? IndexFlags.NoIndexSignatures : IndexFlags.None), constraintType, InferencePriority.MappedTypeConstraint); // If K is constrained to a type C, also infer to C. Thus, for a mapped type { [P in K]: X }, // where K extends keyof T, we make the same inferences as for a homomorphic mapped type // { [P in keyof T]: X }. This enables us to make meaningful inferences when the target is a diff --git a/tests/baselines/reference/objectRestBindingContextualInference.symbols b/tests/baselines/reference/objectRestBindingContextualInference.symbols new file mode 100644 index 0000000000000..73ec44f59a7c7 --- /dev/null +++ b/tests/baselines/reference/objectRestBindingContextualInference.symbols @@ -0,0 +1,76 @@ +=== tests/cases/compiler/objectRestBindingContextualInference.ts === +// slimmed-down repro from #52629 + +type ImageHolder = { +>ImageHolder : Symbol(ImageHolder, Decl(objectRestBindingContextualInference.ts, 0, 0)) +>K : Symbol(K, Decl(objectRestBindingContextualInference.ts, 2, 17)) + + [P in K]: string; +>P : Symbol(P, Decl(objectRestBindingContextualInference.ts, 3, 3)) +>K : Symbol(K, Decl(objectRestBindingContextualInference.ts, 2, 17)) + +}; + +type SetupImageRefs = { +>SetupImageRefs : Symbol(SetupImageRefs, Decl(objectRestBindingContextualInference.ts, 4, 2)) +>K : Symbol(K, Decl(objectRestBindingContextualInference.ts, 6, 20)) + + [P in K]: File; +>P : Symbol(P, Decl(objectRestBindingContextualInference.ts, 7, 3)) +>K : Symbol(K, Decl(objectRestBindingContextualInference.ts, 6, 20)) +>File : Symbol(File, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) + +}; + +type SetupImages = SetupImageRefs & { +>SetupImages : Symbol(SetupImages, Decl(objectRestBindingContextualInference.ts, 8, 2)) +>K : Symbol(K, Decl(objectRestBindingContextualInference.ts, 10, 17)) +>SetupImageRefs : Symbol(SetupImageRefs, Decl(objectRestBindingContextualInference.ts, 4, 2)) +>K : Symbol(K, Decl(objectRestBindingContextualInference.ts, 10, 17)) + + prepare: () => { type: K }; +>prepare : Symbol(prepare, Decl(objectRestBindingContextualInference.ts, 10, 58)) +>type : Symbol(type, Decl(objectRestBindingContextualInference.ts, 11, 18)) +>K : Symbol(K, Decl(objectRestBindingContextualInference.ts, 10, 17)) + +}; + +interface TestInterface { +>TestInterface : Symbol(TestInterface, Decl(objectRestBindingContextualInference.ts, 12, 2)) + + name: string; +>name : Symbol(TestInterface.name, Decl(objectRestBindingContextualInference.ts, 14, 25)) + + image: string; +>image : Symbol(TestInterface.image, Decl(objectRestBindingContextualInference.ts, 15, 15)) +} + +declare function setupImages, K extends string>( +>setupImages : Symbol(setupImages, Decl(objectRestBindingContextualInference.ts, 17, 1)) +>R : Symbol(R, Decl(objectRestBindingContextualInference.ts, 19, 29)) +>ImageHolder : Symbol(ImageHolder, Decl(objectRestBindingContextualInference.ts, 0, 0)) +>K : Symbol(K, Decl(objectRestBindingContextualInference.ts, 19, 54)) +>K : Symbol(K, Decl(objectRestBindingContextualInference.ts, 19, 54)) + + item: R, +>item : Symbol(item, Decl(objectRestBindingContextualInference.ts, 19, 73)) +>R : Symbol(R, Decl(objectRestBindingContextualInference.ts, 19, 29)) + + keys: K[] +>keys : Symbol(keys, Decl(objectRestBindingContextualInference.ts, 20, 10)) +>K : Symbol(K, Decl(objectRestBindingContextualInference.ts, 19, 54)) + +): SetupImages; +>SetupImages : Symbol(SetupImages, Decl(objectRestBindingContextualInference.ts, 8, 2)) +>K : Symbol(K, Decl(objectRestBindingContextualInference.ts, 19, 54)) + +declare const test: TestInterface; +>test : Symbol(test, Decl(objectRestBindingContextualInference.ts, 24, 13)) +>TestInterface : Symbol(TestInterface, Decl(objectRestBindingContextualInference.ts, 12, 2)) + +const { prepare, ...rest } = setupImages(test, ["image"]); +>prepare : Symbol(prepare, Decl(objectRestBindingContextualInference.ts, 26, 7)) +>rest : Symbol(rest, Decl(objectRestBindingContextualInference.ts, 26, 16)) +>setupImages : Symbol(setupImages, Decl(objectRestBindingContextualInference.ts, 17, 1)) +>test : Symbol(test, Decl(objectRestBindingContextualInference.ts, 24, 13)) + diff --git a/tests/baselines/reference/objectRestBindingContextualInference.types b/tests/baselines/reference/objectRestBindingContextualInference.types new file mode 100644 index 0000000000000..a4c6cdbf0c423 --- /dev/null +++ b/tests/baselines/reference/objectRestBindingContextualInference.types @@ -0,0 +1,55 @@ +=== tests/cases/compiler/objectRestBindingContextualInference.ts === +// slimmed-down repro from #52629 + +type ImageHolder = { +>ImageHolder : ImageHolder + + [P in K]: string; +}; + +type SetupImageRefs = { +>SetupImageRefs : SetupImageRefs + + [P in K]: File; +}; + +type SetupImages = SetupImageRefs & { +>SetupImages : SetupImages + + prepare: () => { type: K }; +>prepare : () => { type: K;} +>type : K + +}; + +interface TestInterface { + name: string; +>name : string + + image: string; +>image : string +} + +declare function setupImages, K extends string>( +>setupImages : , K extends string>(item: R, keys: K[]) => SetupImages + + item: R, +>item : R + + keys: K[] +>keys : K[] + +): SetupImages; + +declare const test: TestInterface; +>test : TestInterface + +const { prepare, ...rest } = setupImages(test, ["image"]); +>prepare : () => { type: "image"; } +>rest : { image: File; } +>setupImages(test, ["image"]) : SetupImages<"image"> +>setupImages : , K extends string>(item: R, keys: K[]) => SetupImages +>test : TestInterface +>["image"] : "image"[] +>"image" : "image" + diff --git a/tests/cases/compiler/objectRestBindingContextualInference.ts b/tests/cases/compiler/objectRestBindingContextualInference.ts new file mode 100644 index 0000000000000..703a574cf271f --- /dev/null +++ b/tests/cases/compiler/objectRestBindingContextualInference.ts @@ -0,0 +1,30 @@ +// @strict: true +// @noEmit: true + +// slimmed-down repro from #52629 + +type ImageHolder = { + [P in K]: string; +}; + +type SetupImageRefs = { + [P in K]: File; +}; + +type SetupImages = SetupImageRefs & { + prepare: () => { type: K }; +}; + +interface TestInterface { + name: string; + image: string; +} + +declare function setupImages, K extends string>( + item: R, + keys: K[] +): SetupImages; + +declare const test: TestInterface; + +const { prepare, ...rest } = setupImages(test, ["image"]);