Skip to content
Closed
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
20 changes: 12 additions & 8 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6665,14 +6665,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return createElidedInformationPlaceholder(context);
}
}
else {
return visitAndTransformType(type, createTypeNodeFromObjectType);
}
}
else {
// Anonymous types without a symbol are never circular.
return createTypeNodeFromObjectType(type);
else if (context.visitedTypes?.has(typeId)) {
return createElidedInformationPlaceholder(context);
}

return visitAndTransformType(type, createTypeNodeFromObjectType);

function shouldWriteTypeOfFunctionSymbol() {
const isStaticMethodSymbol = !!(symbol.flags & SymbolFlags.Method) && // typeof static method
some(symbol.declarations, declaration => isStatic(declaration));
Expand Down Expand Up @@ -6726,14 +6725,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
context.symbolDepth!.set(id, depth + 1);
}
context.visitedTypes.add(typeId);
const typeAlreadyVisited = context.visitedTypes.has(typeId);
if (!typeAlreadyVisited) {
context.visitedTypes.add(typeId);
}
const startLength = context.approximateLength;
const result = transform(type);
const addedLength = context.approximateLength - startLength;
if (!context.reportedDiagnostic && !context.encounteredError) {
links?.serializedTypes?.set(key, { node: result, truncating: context.truncating, addedLength });
}
context.visitedTypes.delete(typeId);
if (!typeAlreadyVisited) {
context.visitedTypes.delete(typeId);
}
if (id) {
context.symbolDepth!.set(id, depth!);
}
Expand Down

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions tests/baselines/reference/mappedTypeRecursiveInference.types

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions tests/baselines/reference/selfReferentialFunctionType.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
tests/cases/compiler/selfReferentialFunctionType.ts(1,46): error TS2322: Type '(args: any) => T' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to '(args: any) => T'.


==== tests/cases/compiler/selfReferentialFunctionType.ts (1 errors) ====
function f<T>(args: typeof f<T>): T { return args; }
~~~~
!!! error TS2322: Type '(args: any) => T' is not assignable to type 'T'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to '(args: any) => T'.
!!! related TS6212 tests/cases/compiler/selfReferentialFunctionType.ts:1:46: Did you mean to call this expression?

function g<T = typeof g>(args: T): T { return args; }

function h<T>(): typeof h<T> { return h; }

19 changes: 19 additions & 0 deletions tests/baselines/reference/selfReferentialFunctionType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [selfReferentialFunctionType.ts]
function f<T>(args: typeof f<T>): T { return args; }

function g<T = typeof g>(args: T): T { return args; }

function h<T>(): typeof h<T> { return h; }


//// [selfReferentialFunctionType.js]
"use strict";
function f(args) { return args; }
function g(args) { return args; }
function h() { return h; }


//// [selfReferentialFunctionType.d.ts]
declare function f<T>(args: typeof f<T>): T;
declare function g<T = typeof g>(args: T): T;
declare function h<T>(): typeof h<T>;
26 changes: 26 additions & 0 deletions tests/baselines/reference/selfReferentialFunctionType.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
=== tests/cases/compiler/selfReferentialFunctionType.ts ===
function f<T>(args: typeof f<T>): T { return args; }
>f : Symbol(f, Decl(selfReferentialFunctionType.ts, 0, 0))
>T : Symbol(T, Decl(selfReferentialFunctionType.ts, 0, 11))
>args : Symbol(args, Decl(selfReferentialFunctionType.ts, 0, 14))
>f : Symbol(f, Decl(selfReferentialFunctionType.ts, 0, 0))
>T : Symbol(T, Decl(selfReferentialFunctionType.ts, 0, 11))
>T : Symbol(T, Decl(selfReferentialFunctionType.ts, 0, 11))
>args : Symbol(args, Decl(selfReferentialFunctionType.ts, 0, 14))

function g<T = typeof g>(args: T): T { return args; }
>g : Symbol(g, Decl(selfReferentialFunctionType.ts, 0, 52))
>T : Symbol(T, Decl(selfReferentialFunctionType.ts, 2, 11))
>g : Symbol(g, Decl(selfReferentialFunctionType.ts, 0, 52))
>args : Symbol(args, Decl(selfReferentialFunctionType.ts, 2, 25))
>T : Symbol(T, Decl(selfReferentialFunctionType.ts, 2, 11))
>T : Symbol(T, Decl(selfReferentialFunctionType.ts, 2, 11))
>args : Symbol(args, Decl(selfReferentialFunctionType.ts, 2, 25))

function h<T>(): typeof h<T> { return h; }
>h : Symbol(h, Decl(selfReferentialFunctionType.ts, 2, 53))
>T : Symbol(T, Decl(selfReferentialFunctionType.ts, 4, 11))
>h : Symbol(h, Decl(selfReferentialFunctionType.ts, 2, 53))
>T : Symbol(T, Decl(selfReferentialFunctionType.ts, 4, 11))
>h : Symbol(h, Decl(selfReferentialFunctionType.ts, 2, 53))

18 changes: 18 additions & 0 deletions tests/baselines/reference/selfReferentialFunctionType.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/selfReferentialFunctionType.ts ===
function f<T>(args: typeof f<T>): T { return args; }
>f : <T>(args: typeof f<T>) => T
>args : (args: typeof f<T>) => T
>f : <T>(args: (args: any) => T) => T
>args : (args: any) => T

function g<T = typeof g>(args: T): T { return args; }
>g : <T = typeof g>(args: T) => T
>g : <T = typeof g>(args: T) => T
>args : T
>args : T

function h<T>(): typeof h<T> { return h; }
>h : <T>() => typeof h<T>
>h : <T>() => typeof h<T>
>h : <T>() => () => any

8 changes: 8 additions & 0 deletions tests/cases/compiler/selfReferentialFunctionType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @strict: true
// @declaration: true

function f<T>(args: typeof f<T>): T { return args; }

function g<T = typeof g>(args: T): T { return args; }

function h<T>(): typeof h<T> { return h; }
12 changes: 3 additions & 9 deletions tests/cases/fourslash/quickInfoMappedTypeRecursiveInference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,13 @@ verify.quickInfoAt('2', `function foo<{
};
}`);
verify.quickInfoAt('3', `(property) a: {
a: {
a: ...;
};
a: ...;
}`);
verify.quickInfoAt('4', `(property) a: {
a: {
a: ...;
};
a: ...;
}`);
verify.quickInfoAt('5', `(property) a: {
a: {
a: ...;
};
a: ...;
}`);
verify.quickInfoAt('6', `const oub: {
[x: string]: ...;
Expand Down