Skip to content

Flag mapped types with circular property types and do not attempt to print them structurally #39552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2020
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
5 changes: 3 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4572,8 +4572,8 @@ namespace ts {
}

function createTypeNodeFromObjectType(type: ObjectType): TypeNode {
if (isGenericMappedType(type)) {
return createMappedTypeNodeFromType(type);
if (isGenericMappedType(type) || (type as MappedType).containsError) {
return createMappedTypeNodeFromType(type as MappedType);
}

const resolved = resolveStructuredTypeMembers(type);
Expand Down Expand Up @@ -10309,6 +10309,7 @@ namespace ts {
function getTypeOfMappedSymbol(symbol: MappedSymbol) {
if (!symbol.type) {
if (!pushTypeResolution(symbol, TypeSystemPropertyName.Type)) {
symbol.mappedType.containsError = true;
return errorType;
}
const templateType = getTemplateTypeFromMappedType(<MappedType>symbol.mappedType.target || symbol.mappedType);
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5168,6 +5168,7 @@ namespace ts {
templateType?: Type;
modifiersType?: Type;
resolvedApparentType?: Type;
containsError?: boolean;
}

export interface EvolvingArrayType extends ObjectType {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
tests/cases/compiler/recursivelyExpandingUnionNoStackoverflow.ts(3,10): error TS2589: Type instantiation is excessively deep and possibly infinite.
tests/cases/compiler/recursivelyExpandingUnionNoStackoverflow.ts(3,10): error TS2615: Type of property 'M' circularly references itself in mapped type '{ [P in "M"]: any; }'.


==== tests/cases/compiler/recursivelyExpandingUnionNoStackoverflow.ts (2 errors) ====
type N<T, K extends string> = T | { [P in K]: N<T, K> }[K];

type M = N<number, "M">;
~~~~~~~~~~~~~~
!!! error TS2589: Type instantiation is excessively deep and possibly infinite.
~~~~~~~~~~~~~~
!!! error TS2615: Type of property 'M' circularly references itself in mapped type '{ [P in "M"]: any; }'.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//// [recursivelyExpandingUnionNoStackoverflow.ts]
type N<T, K extends string> = T | { [P in K]: N<T, K> }[K];

type M = N<number, "M">;

//// [recursivelyExpandingUnionNoStackoverflow.js]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/recursivelyExpandingUnionNoStackoverflow.ts ===
type N<T, K extends string> = T | { [P in K]: N<T, K> }[K];
>N : Symbol(N, Decl(recursivelyExpandingUnionNoStackoverflow.ts, 0, 0))
>T : Symbol(T, Decl(recursivelyExpandingUnionNoStackoverflow.ts, 0, 7))
>K : Symbol(K, Decl(recursivelyExpandingUnionNoStackoverflow.ts, 0, 9))
>T : Symbol(T, Decl(recursivelyExpandingUnionNoStackoverflow.ts, 0, 7))
>P : Symbol(P, Decl(recursivelyExpandingUnionNoStackoverflow.ts, 0, 37))
>K : Symbol(K, Decl(recursivelyExpandingUnionNoStackoverflow.ts, 0, 9))
>N : Symbol(N, Decl(recursivelyExpandingUnionNoStackoverflow.ts, 0, 0))
>T : Symbol(T, Decl(recursivelyExpandingUnionNoStackoverflow.ts, 0, 7))
>K : Symbol(K, Decl(recursivelyExpandingUnionNoStackoverflow.ts, 0, 9))
>K : Symbol(K, Decl(recursivelyExpandingUnionNoStackoverflow.ts, 0, 9))

type M = N<number, "M">;
>M : Symbol(M, Decl(recursivelyExpandingUnionNoStackoverflow.ts, 0, 59))
>N : Symbol(N, Decl(recursivelyExpandingUnionNoStackoverflow.ts, 0, 0))

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
=== tests/cases/compiler/recursivelyExpandingUnionNoStackoverflow.ts ===
type N<T, K extends string> = T | { [P in K]: N<T, K> }[K];
>N : N<T, K>

type M = N<number, "M">;
>M : any

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type N<T, K extends string> = T | { [P in K]: N<T, K> }[K];

type M = N<number, "M">;