You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Return type is any of getter in object passed as generic #49511
declarefunctiongeneric<T>(v: T): T;// Passing object directly to genery gives type `any` on `get hi()`conststore=generic({hello: "hi",// return type of getter is anygethi(){returnthis.hello;},// If you remove this function, return type of getter will be stringdoSomething(){}});constres=store.hi// any// Passing object directly to genery gives type `any` on `get hi()`constobj={hello: "hi",// return type of getter is stringgethi(){returnthis.hello;},doSomething(){}};conststore2=generic(obj)constres2=store2.hi// string
🙁 Actual behavior
Return type of getters are any
🙂 Expected behavior
Return type of getters have the type of the returned data
Looks related -- from src/compiler/checker.ts, resolveCallExpression(...)
// When a call to a generic function is an argument to an outer call to a generic function for which
// inference is in process, we have a choice to make. If the inner call relies on inferences made from
// its contextual type to its return type, deferring the inner call processing allows the best possible
// contextual type to accumulate. But if the outer call relies on inferences made from the return type of
// the inner call, the inner call should be processed early. There's no sure way to know which choice is
// right (only a full unification algorithm can determine that), so we resort to the following heuristic:
// If no type arguments are specified in the inner call and at least one call signature is generic and
// returns a function type, we choose to defer processing. This narrowly permits function composition
// operators to flow inferences through return types, but otherwise processes calls right away. We
// use the resolvingSignature singleton to indicate that we deferred processing. This result will be
// propagated out and eventually turned into nonInferrableType (a type that is assignable to anything and
// from which we never make inferences).
if (checkMode & CheckMode.SkipGenericFunctions
&& !node.typeArguments
&& callSignatures.some(isGenericFunctionReturningFunction)) {
skippedGenericFunction(node, checkMode);
return resolvingSignature;
}
This is fixed by PR #62243. With --target es6 --strict, 4.7.2 and 6.0.0-dev.20251210 infer any for the inline getter, while 6.0.0-dev.20251211, TypeScript 6.0.3, and the current native compiler infer string for both the inline and stored-object getters.
Activity
craigphicks commentedon Jun 14, 2022
Looks related -- from src/compiler/checker.ts,
resolveCallExpression(...)lqzhgood commentedon May 9, 2024
#58483
RyanCavanaugh commentedon Sep 9, 2026
This is fixed by PR #62243. With
--target es6 --strict, 4.7.2 and 6.0.0-dev.20251210 inferanyfor the inline getter, while 6.0.0-dev.20251211, TypeScript 6.0.3, and the current native compiler inferstringfor both the inline and stored-object getters.