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
10 changes: 9 additions & 1 deletion tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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> = R extends Stubable ? Provider<R> : Promise<R>;
>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> = V extends (...args: infer P) => infer R ? (...args: P) => Result<R> : Result<V>;
>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> = T extends Stubable ? MethodOrProperty<T> : 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<T extends Stubable>(value: T): Provider<T>;
>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))

Original file line number Diff line number Diff line change
@@ -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> = R extends Stubable ? Provider<R> : Promise<R>;
>Result : Result<R>

type MethodOrProperty<V> = V extends (...args: infer P) => infer R ? (...args: P) => Result<R> : Result<V>;
>MethodOrProperty : MethodOrProperty<V>
>args : P
>args : P

type Provider<T> = T extends Stubable ? MethodOrProperty<T> : unknown;
>Provider : Provider<T>

export function stub<T extends Stubable>(value: T): Provider<T>;
>stub : <T extends Stubable>(value: T) => Provider<T>
>value : T
}

const a = import("x").then((m) => 1);
>a : Promise<number>
>import("x").then((m) => 1) : Promise<number>
>import("x").then : <TResult1 = { stub<T extends import("x").Stubable>(value: T): import("x").Provider<T>; default: typeof import("x"); }, TResult2 = never>(onfulfilled?: ((value: { stub<T extends import("x").Stubable>(value: T): import("x").Provider<T>; default: typeof import("x"); }) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>
>import("x") : Promise<{ stub<T extends import("x").Stubable>(value: T): import("x").Provider<T>; default: typeof import("x"); }>
>"x" : "x"
>then : <TResult1 = { stub<T extends import("x").Stubable>(value: T): import("x").Provider<T>; default: typeof import("x"); }, TResult2 = never>(onfulfilled?: ((value: { stub<T extends import("x").Stubable>(value: T): import("x").Provider<T>; default: typeof import("x"); }) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>
>(m) => 1 : (m: { stub<T extends import("x").Stubable>(value: T): import("x").Provider<T>; default: typeof import("x"); }) => number
>m : { stub<T extends import("x").Stubable>(value: T): import("x").Provider<T>; default: typeof import("x"); }
>1 : 1

Original file line number Diff line number Diff line change
@@ -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> = R extends Stubable ? Provider<R> : Promise<R>;
type MethodOrProperty<V> = V extends (...args: infer P) => infer R ? (...args: P) => Result<R> : Result<V>;
type Provider<T> = T extends Stubable ? MethodOrProperty<T> : unknown;
export function stub<T extends Stubable>(value: T): Provider<T>;
}

const a = import("x").then((m) => 1);
Loading