From 1040118c8b954896ae04f7226557683b8a0c28ab Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:13:00 -0800 Subject: [PATCH 1/2] Add test for inference from the type of a dynamic import For 65445 --- ...heticDefaultImportTypeInference.errors.txt | 23 +++++++ ...yntheticDefaultImportTypeInference.symbols | 66 +++++++++++++++++++ .../syntheticDefaultImportTypeInference.types | 44 +++++++++++++ .../syntheticDefaultImportTypeInference.ts | 22 +++++++ 4 files changed, 155 insertions(+) create mode 100644 tsc/testdata/baselines/reference/compiler/syntheticDefaultImportTypeInference.errors.txt create mode 100644 tsc/testdata/baselines/reference/compiler/syntheticDefaultImportTypeInference.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/syntheticDefaultImportTypeInference.types create mode 100644 tsc/testdata/tests/cases/compiler/syntheticDefaultImportTypeInference.ts diff --git a/tsc/testdata/baselines/reference/compiler/syntheticDefaultImportTypeInference.errors.txt b/tsc/testdata/baselines/reference/compiler/syntheticDefaultImportTypeInference.errors.txt new file mode 100644 index 0000000000000..6f89e7170d334 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/syntheticDefaultImportTypeInference.errors.txt @@ -0,0 +1,23 @@ +syntheticDefaultImportTypeInference.ts(16,11): error TS5115: Instantiations of the following types appear infinitely circular: 'MethodOrProperty', 'Provider'. + + +==== syntheticDefaultImportTypeInference.ts (1 errors) ==== + // https://github.com/microsoft/TypeScript/issues/65445 + + // The type of import("x") is the module's type with a synthetic default, + // which cannot reference type parameters, so inference from the callback + // does not explore its members, among them stub, whose return type is + // infinite for an erased type argument. + + declare module "x" { + type Stubable = (...args: any[]) => any; + type Result = R extends Stubable ? Provider : Promise; + type MethodOrProperty = V extends (...args: infer P) => infer R ? (...args: P) => Result : Result; + type Provider = T extends Stubable ? MethodOrProperty : unknown; + export function stub(value: T): Provider; + } + + const a = import("x").then((m) => 1); + ~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS5115: Instantiations of the following types appear infinitely circular: 'MethodOrProperty', 'Provider'. + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/syntheticDefaultImportTypeInference.symbols b/tsc/testdata/baselines/reference/compiler/syntheticDefaultImportTypeInference.symbols new file mode 100644 index 0000000000000..9cc3142a58e66 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/syntheticDefaultImportTypeInference.symbols @@ -0,0 +1,66 @@ +//// [tests/cases/compiler/syntheticDefaultImportTypeInference.ts] //// + +=== syntheticDefaultImportTypeInference.ts === +// https://github.com/microsoft/TypeScript/issues/65445 + +// The type of import("x") is the module's type with a synthetic default, +// which cannot reference type parameters, so inference from the callback +// does not explore its members, among them stub, whose return type is +// infinite for an erased type argument. + +declare module "x" { +>"x" : Symbol("x", Decl(syntheticDefaultImportTypeInference.ts, 0, 0)) + + type Stubable = (...args: any[]) => any; +>Stubable : Symbol(Stubable, Decl(syntheticDefaultImportTypeInference.ts, 7, 20)) +>args : Symbol(args, Decl(syntheticDefaultImportTypeInference.ts, 8, 21)) + + type Result = R extends Stubable ? Provider : Promise; +>Result : Symbol(Result, Decl(syntheticDefaultImportTypeInference.ts, 8, 44)) +>R : Symbol(R, Decl(syntheticDefaultImportTypeInference.ts, 9, 16)) +>R : Symbol(R, Decl(syntheticDefaultImportTypeInference.ts, 9, 16)) +>Stubable : Symbol(Stubable, Decl(syntheticDefaultImportTypeInference.ts, 7, 20)) +>Provider : Symbol(Provider, Decl(syntheticDefaultImportTypeInference.ts, 10, 111)) +>R : Symbol(R, Decl(syntheticDefaultImportTypeInference.ts, 9, 16)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) +>R : Symbol(R, Decl(syntheticDefaultImportTypeInference.ts, 9, 16)) + + type MethodOrProperty = V extends (...args: infer P) => infer R ? (...args: P) => Result : Result; +>MethodOrProperty : Symbol(MethodOrProperty, Decl(syntheticDefaultImportTypeInference.ts, 9, 67)) +>V : Symbol(V, Decl(syntheticDefaultImportTypeInference.ts, 10, 26)) +>V : Symbol(V, Decl(syntheticDefaultImportTypeInference.ts, 10, 26)) +>args : Symbol(args, Decl(syntheticDefaultImportTypeInference.ts, 10, 42)) +>P : Symbol(P, Decl(syntheticDefaultImportTypeInference.ts, 10, 56)) +>R : Symbol(R, Decl(syntheticDefaultImportTypeInference.ts, 10, 68)) +>args : Symbol(args, Decl(syntheticDefaultImportTypeInference.ts, 10, 74)) +>P : Symbol(P, Decl(syntheticDefaultImportTypeInference.ts, 10, 56)) +>Result : Symbol(Result, Decl(syntheticDefaultImportTypeInference.ts, 8, 44)) +>R : Symbol(R, Decl(syntheticDefaultImportTypeInference.ts, 10, 68)) +>Result : Symbol(Result, Decl(syntheticDefaultImportTypeInference.ts, 8, 44)) +>V : Symbol(V, Decl(syntheticDefaultImportTypeInference.ts, 10, 26)) + + type Provider = T extends Stubable ? MethodOrProperty : unknown; +>Provider : Symbol(Provider, Decl(syntheticDefaultImportTypeInference.ts, 10, 111)) +>T : Symbol(T, Decl(syntheticDefaultImportTypeInference.ts, 11, 18)) +>T : Symbol(T, Decl(syntheticDefaultImportTypeInference.ts, 11, 18)) +>Stubable : Symbol(Stubable, Decl(syntheticDefaultImportTypeInference.ts, 7, 20)) +>MethodOrProperty : Symbol(MethodOrProperty, Decl(syntheticDefaultImportTypeInference.ts, 9, 67)) +>T : Symbol(T, Decl(syntheticDefaultImportTypeInference.ts, 11, 18)) + + export function stub(value: T): Provider; +>stub : Symbol(stub, Decl(syntheticDefaultImportTypeInference.ts, 11, 74)) +>T : Symbol(T, Decl(syntheticDefaultImportTypeInference.ts, 12, 25)) +>Stubable : Symbol(Stubable, Decl(syntheticDefaultImportTypeInference.ts, 7, 20)) +>value : Symbol(value, Decl(syntheticDefaultImportTypeInference.ts, 12, 45)) +>T : Symbol(T, Decl(syntheticDefaultImportTypeInference.ts, 12, 25)) +>Provider : Symbol(Provider, Decl(syntheticDefaultImportTypeInference.ts, 10, 111)) +>T : Symbol(T, Decl(syntheticDefaultImportTypeInference.ts, 12, 25)) +} + +const a = import("x").then((m) => 1); +>a : Symbol(a, Decl(syntheticDefaultImportTypeInference.ts, 15, 5)) +>import("x").then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --)) +>"x" : Symbol("x", Decl(syntheticDefaultImportTypeInference.ts, 0, 0)) +>then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --)) +>m : Symbol(m, Decl(syntheticDefaultImportTypeInference.ts, 15, 28)) + diff --git a/tsc/testdata/baselines/reference/compiler/syntheticDefaultImportTypeInference.types b/tsc/testdata/baselines/reference/compiler/syntheticDefaultImportTypeInference.types new file mode 100644 index 0000000000000..e704e3853a085 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/syntheticDefaultImportTypeInference.types @@ -0,0 +1,44 @@ +//// [tests/cases/compiler/syntheticDefaultImportTypeInference.ts] //// + +=== syntheticDefaultImportTypeInference.ts === +// https://github.com/microsoft/TypeScript/issues/65445 + +// The type of import("x") is the module's type with a synthetic default, +// which cannot reference type parameters, so inference from the callback +// does not explore its members, among them stub, whose return type is +// infinite for an erased type argument. + +declare module "x" { +>"x" : typeof import("x") + + type Stubable = (...args: any[]) => any; +>Stubable : Stubable +>args : any[] + + type Result = R extends Stubable ? Provider : Promise; +>Result : Result + + type MethodOrProperty = V extends (...args: infer P) => infer R ? (...args: P) => Result : Result; +>MethodOrProperty : MethodOrProperty +>args : P +>args : P + + type Provider = T extends Stubable ? MethodOrProperty : unknown; +>Provider : Provider + + export function stub(value: T): Provider; +>stub : (value: T) => Provider +>value : T +} + +const a = import("x").then((m) => 1); +>a : Promise +>import("x").then((m) => 1) : Promise +>import("x").then : (value: T): import("x").Provider; default: typeof import("x"); }, TResult2 = never>(onfulfilled?: ((value: { stub(value: T): import("x").Provider; default: typeof import("x"); }) => TResult1 | PromiseLike) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike) | null | undefined) => Promise +>import("x") : Promise<{ stub(value: T): import("x").Provider; default: typeof import("x"); }> +>"x" : "x" +>then : (value: T): import("x").Provider; default: typeof import("x"); }, TResult2 = never>(onfulfilled?: ((value: { stub(value: T): import("x").Provider; default: typeof import("x"); }) => TResult1 | PromiseLike) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike) | null | undefined) => Promise +>(m) => 1 : (m: { stub(value: T): import("x").Provider; default: typeof import("x"); }) => number +>m : { stub(value: T): import("x").Provider; default: typeof import("x"); } +>1 : 1 + diff --git a/tsc/testdata/tests/cases/compiler/syntheticDefaultImportTypeInference.ts b/tsc/testdata/tests/cases/compiler/syntheticDefaultImportTypeInference.ts new file mode 100644 index 0000000000000..f33cf98dbf656 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/syntheticDefaultImportTypeInference.ts @@ -0,0 +1,22 @@ +// @strict: true +// @noEmit: true +// @target: es2022 +// @module: esnext +// @moduleResolution: bundler + +// https://github.com/microsoft/TypeScript/issues/65445 + +// The type of import("x") is the module's type with a synthetic default, +// which cannot reference type parameters, so inference from the callback +// does not explore its members, among them stub, whose return type is +// infinite for an erased type argument. + +declare module "x" { + type Stubable = (...args: any[]) => any; + type Result = R extends Stubable ? Provider : Promise; + type MethodOrProperty = V extends (...args: infer P) => infer R ? (...args: P) => Result : Result; + type Provider = T extends Stubable ? MethodOrProperty : unknown; + export function stub(value: T): Provider; +} + +const a = import("x").then((m) => 1); From d10541dbf5da832d18eb1ddbddad34ba36fbcccc Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:18:36 -0800 Subject: [PATCH 2/2] Do not infer into the type wrapping a module for a default import The object type wrapping a module for a synthetic default import has the module's declarations, so that types are ordered the same way wherever it is compared. Having declarations made it an anonymous type that could reference type parameters, so inference explored its members. For import("cloudflare:workers").then(mod => mod), inferring from the callback erased the type parameter of a generic function the module exports, and instantiating its return type with any never ended, reporting TS5115. A module is never within the scope of a type parameter, so the wrapper is again known not to contain any. Fixes 65445 --- tsc/internal/checker/checker.go | 10 +++++++- ...heticDefaultImportTypeInference.errors.txt | 23 ------------------- 2 files changed, 9 insertions(+), 24 deletions(-) delete mode 100644 tsc/testdata/baselines/reference/compiler/syntheticDefaultImportTypeInference.errors.txt diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index f1e5564580f3c..8ca4cab947be2 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -16205,6 +16205,14 @@ func (c *Checker) createDefaultPropertyWrapperForModule(symbol *ast.Symbol, orig return c.newAnonymousType(anonymousSymbol, memberTable, nil, nil, nil) } +// isModuleWrapperSymbol reports whether symbol is that of the object type +// wrapping a module for a synthetic default import, which has the module's +// declarations. A module is never within the scope of a type parameter. +func isModuleWrapperSymbol(symbol *ast.Symbol) bool { + d := symbol.Declarations()[0] + return ast.IsSourceFile(d) || ast.IsModuleDeclaration(d) +} + func (c *Checker) cloneTypeAsModuleType(symbol *ast.Symbol, moduleType *Type, referenceParent *ast.Node) *ast.Symbol { result := c.newSymbol(symbol.Flags(), symbol.Name()) result.SetDeclarations(slices.Clone(symbol.Declarations())) @@ -22937,7 +22945,7 @@ func (c *Checker) couldContainTypeVariablesWorker(t *Type) bool { } result := t.flags&TypeFlagsInstantiable != 0 || t.flags&TypeFlagsObject != 0 && !c.isNonGenericTopLevelType(t) && (objectFlags&ObjectFlagsReference != 0 && (t.AsTypeReference().node != nil || core.Some(c.getTypeArguments(t), c.couldContainTypeVariables)) || - objectFlags&ObjectFlagsAnonymous != 0 && t.symbol != nil && t.symbol.Flags()&(ast.SymbolFlagsFunction|ast.SymbolFlagsMethod|ast.SymbolFlagsClass|ast.SymbolFlagsTypeLiteral|ast.SymbolFlagsObjectLiteral) != 0 && t.symbol.Declarations() != nil || + objectFlags&ObjectFlagsAnonymous != 0 && t.symbol != nil && t.symbol.Flags()&(ast.SymbolFlagsFunction|ast.SymbolFlagsMethod|ast.SymbolFlagsClass|ast.SymbolFlagsTypeLiteral|ast.SymbolFlagsObjectLiteral) != 0 && t.symbol.Declarations() != nil && !isModuleWrapperSymbol(t.symbol) || objectFlags&(ObjectFlagsMapped|ObjectFlagsReverseMapped|ObjectFlagsObjectRestType|ObjectFlagsInstantiationExpressionType) != 0) || t.flags&TypeFlagsUnionOrIntersection != 0 && t.flags&TypeFlagsEnumLiteral == 0 && !c.isNonGenericTopLevelType(t) && core.Some(t.Types(), c.couldContainTypeVariables) t.objectFlags |= ObjectFlagsCouldContainTypeVariablesComputed | core.IfElse(result, ObjectFlagsCouldContainTypeVariables, 0) diff --git a/tsc/testdata/baselines/reference/compiler/syntheticDefaultImportTypeInference.errors.txt b/tsc/testdata/baselines/reference/compiler/syntheticDefaultImportTypeInference.errors.txt deleted file mode 100644 index 6f89e7170d334..0000000000000 --- a/tsc/testdata/baselines/reference/compiler/syntheticDefaultImportTypeInference.errors.txt +++ /dev/null @@ -1,23 +0,0 @@ -syntheticDefaultImportTypeInference.ts(16,11): error TS5115: Instantiations of the following types appear infinitely circular: 'MethodOrProperty', 'Provider'. - - -==== syntheticDefaultImportTypeInference.ts (1 errors) ==== - // https://github.com/microsoft/TypeScript/issues/65445 - - // The type of import("x") is the module's type with a synthetic default, - // which cannot reference type parameters, so inference from the callback - // does not explore its members, among them stub, whose return type is - // infinite for an erased type argument. - - declare module "x" { - type Stubable = (...args: any[]) => any; - type Result = R extends Stubable ? Provider : Promise; - type MethodOrProperty = V extends (...args: infer P) => infer R ? (...args: P) => Result : Result; - type Provider = T extends Stubable ? MethodOrProperty : unknown; - export function stub(value: T): Provider; - } - - const a = import("x").then((m) => 1); - ~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS5115: Instantiations of the following types appear infinitely circular: 'MethodOrProperty', 'Provider'. - \ No newline at end of file