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.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);