Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8586,19 +8586,6 @@ namespace ts {
}
}
else {
if (getObjectFlags(target) & ObjectFlags.Mapped) {
const constraintType = getConstraintTypeFromMappedType(<MappedType>target);
if (getObjectFlags(source) & ObjectFlags.Mapped) {
inferFromTypes(getConstraintTypeFromMappedType(<MappedType>source), constraintType);
inferFromTypes(getTemplateTypeFromMappedType(<MappedType>source), getTemplateTypeFromMappedType(<MappedType>target));
return;
}
if (constraintType.flags & TypeFlags.TypeParameter) {
inferFromTypes(getIndexType(source), constraintType);
inferFromTypes(getUnionType(map(getPropertiesOfType(source), getTypeOfSymbol)), getTemplateTypeFromMappedType(<MappedType>target));
return;
}
}
source = getApparentType(source);
if (source.flags & TypeFlags.Object) {
if (isInProcess(source, target)) {
Expand All @@ -8619,15 +8606,32 @@ namespace ts {
sourceStack[depth] = source;
targetStack[depth] = target;
depth++;
inferFromProperties(source, target);
inferFromSignatures(source, target, SignatureKind.Call);
inferFromSignatures(source, target, SignatureKind.Construct);
inferFromIndexTypes(source, target);
inferFromObjectTypes(source, target);
depth--;
}
}
}

function inferFromObjectTypes(source: Type, target: Type) {
if (getObjectFlags(target) & ObjectFlags.Mapped) {
const constraintType = getConstraintTypeFromMappedType(<MappedType>target);
if (getObjectFlags(source) & ObjectFlags.Mapped) {
inferFromTypes(getConstraintTypeFromMappedType(<MappedType>source), constraintType);
inferFromTypes(getTemplateTypeFromMappedType(<MappedType>source), getTemplateTypeFromMappedType(<MappedType>target));
return;
}
if (constraintType.flags & TypeFlags.TypeParameter) {
inferFromTypes(getIndexType(source), constraintType);
inferFromTypes(getUnionType(map(getPropertiesOfType(source), getTypeOfSymbol)), getTemplateTypeFromMappedType(<MappedType>target));
return;
}
}
inferFromProperties(source, target);
inferFromSignatures(source, target, SignatureKind.Call);
inferFromSignatures(source, target, SignatureKind.Construct);
inferFromIndexTypes(source, target);
}

function inferFromProperties(source: Type, target: Type) {
const properties = getPropertiesOfObjectType(target);
for (const targetProp of properties) {
Expand Down
12 changes: 12 additions & 0 deletions tests/baselines/reference/mappedTypeInferenceCircularity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//// [mappedTypeInferenceCircularity.ts]
// Repro from #12511

type HTML = { [K in 'div']: Block<HTML> };
type Block<P> = <T>(func: HTML) => {};

declare var h: HTML;
h.div(h);

//// [mappedTypeInferenceCircularity.js]
// Repro from #12511
h.div(h);
26 changes: 26 additions & 0 deletions tests/baselines/reference/mappedTypeInferenceCircularity.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
=== tests/cases/compiler/mappedTypeInferenceCircularity.ts ===
// Repro from #12511

type HTML = { [K in 'div']: Block<HTML> };
>HTML : Symbol(HTML, Decl(mappedTypeInferenceCircularity.ts, 0, 0))
>K : Symbol(K, Decl(mappedTypeInferenceCircularity.ts, 2, 15))
>Block : Symbol(Block, Decl(mappedTypeInferenceCircularity.ts, 2, 42))
>HTML : Symbol(HTML, Decl(mappedTypeInferenceCircularity.ts, 0, 0))

type Block<P> = <T>(func: HTML) => {};
>Block : Symbol(Block, Decl(mappedTypeInferenceCircularity.ts, 2, 42))
>P : Symbol(P, Decl(mappedTypeInferenceCircularity.ts, 3, 11))
>T : Symbol(T, Decl(mappedTypeInferenceCircularity.ts, 3, 17))
>func : Symbol(func, Decl(mappedTypeInferenceCircularity.ts, 3, 20))
>HTML : Symbol(HTML, Decl(mappedTypeInferenceCircularity.ts, 0, 0))

declare var h: HTML;
>h : Symbol(h, Decl(mappedTypeInferenceCircularity.ts, 5, 11))
>HTML : Symbol(HTML, Decl(mappedTypeInferenceCircularity.ts, 0, 0))

h.div(h);
>h.div : Symbol(div)
>h : Symbol(h, Decl(mappedTypeInferenceCircularity.ts, 5, 11))
>div : Symbol(div)
>h : Symbol(h, Decl(mappedTypeInferenceCircularity.ts, 5, 11))

27 changes: 27 additions & 0 deletions tests/baselines/reference/mappedTypeInferenceCircularity.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/mappedTypeInferenceCircularity.ts ===
// Repro from #12511

type HTML = { [K in 'div']: Block<HTML> };
>HTML : HTML
>K : K
>Block : Block<P>
>HTML : HTML

type Block<P> = <T>(func: HTML) => {};
>Block : Block<P>
>P : P
>T : T
>func : HTML
>HTML : HTML

declare var h: HTML;
>h : HTML
>HTML : HTML

h.div(h);
>h.div(h) : {}
>h.div : Block<HTML>
>h : HTML
>div : Block<HTML>
>h : HTML

7 changes: 7 additions & 0 deletions tests/cases/compiler/mappedTypeInferenceCircularity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Repro from #12511

type HTML = { [K in 'div']: Block<HTML> };
type Block<P> = <T>(func: HTML) => {};

declare var h: HTML;
h.div(h);