Repository navigation
Generic type inference failedΒ #56855
Description
Activity
- changed the title
[-]generic type inference failed[/-][+]Generic type inference failed[/+]on Dec 24, 2023 - addedBugA bug in TypeScriptA bug in TypeScriptHelp WantedYou can do thisYou can do this
on Jan 2, 2024 The following seems to work:
function func< T1, T3 extends { [key: string]: (context: T1) => void }, T4 = {}, >(data: { T: T1; K: T3 & ThisType<T1 & T3>; }) { } func({ T: { bbb: 123, aaa: () => { return 333; }, // <-- here lies the difference: arrow instead of method }, K: { bbb(context) { console.log(context.aaa()); console.log(context.bbb); this.ccc(); }, ccc() { }, }, });
Andarist commented
on Jan 4, 2024 ContributorMore actionsThis likely belongs to this umbrella issue: #47599
The problem is the following
ifinsideinstantiateContextualType:// If no inferences have been made, and none of the type parameters for which we are inferring // specify default types, nothing is gained from instantiating as type parameters would just be // replaced with their constraints similar to the apparent type. if ( inferenceContext && contextFlags! & ContextFlags.Signature && some(inferenceContext.inferences, hasInferenceCandidatesOrDefault) ) { // For contextual signatures we incorporate all inferences made so far, e.g. from return // types as well as arguments to the left in a function call. return instantiateInstantiableTypes(contextualType, inferenceContext.nonFixingMapper); }
The condition results
trueat the wrong time, too eagerly as far as I understand it, therefore stuff gets instantiated with some default types.If you change the condition in one of the two following ways you solve this specific issue, but you get failing baselines:
!some(inferenceContext.inferences, hasInferenceCandidatesOrDefault)seems somewhat more related with the above comment (?), but you get too many failing baselinesevery(inferenceContext.inferences, hasInferenceCandidatesOrDefault)just a couple of failing baselines, but one of them is about contextual inference and, guess what, it works here but not there anymore
Reacted by Ryan Cavanaugh and Craig P Hicks- addedDomain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolution
on Oct 16, 2025 This seems to have been fixed by TypeScript 6.0. The provided example no longer errors and
contextis now inferred as{ bbb: number; aaa(): 333 }. I'm guessing that #62243 did the trick.RyanCavanaugh commented
on Sep 19, 2026 MemberMore actionsThis is fixed by PR #62243. The original object-literal method form produced
context: unknownand TS2339 errors in 5.9.3 and6.0.0-dev.20251210; it is clean from6.0.0-dev.20251211, including 6.0.3 and the current native compiler.- addedFixedA PR has been merged for this issueA PR has been merged for this issueNeeds Human ReviewThis issue has a backlog check awaiting maintainer review.This issue has a backlog check awaiting maintainer review.
on Sep 19, 2026 - removedNeeds Human ReviewThis issue has a backlog check awaiting maintainer review.This issue has a backlog check awaiting maintainer review.
on Sep 23, 2026
π Search Terms
Generics
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABKSAeAUIriAqBGAGk2xwGZEBTADygrABMBnRAb0QG0BrCgTwC5EjKACcYYAOYBdAQAoICWjQH4AlIgC8APkQA3ODHqIAvkWy4ALBtYnEAeluIAwnAC2LurAmI4IKIigAFjDMjBTQ8EhwwIjy9BSILgCG3MwwfgDucMKciAAOwnC5FMIANjwAdOiaMvSJUIkCLMRYOMp4ANzNiADSyuQAZLhBjDg8Raj4iINkmp1Gak1G6OgoEDJNZq2sXWYARvsCeABMpKZm2ImXMguIwhRQIMJIpC-txmdYJl292+fY+7s5ApqFAFjs-jEEIw4CUKOUSnBxECwIooOVLolripOhDzvIwNDYfDEcjUeUAdjwX9AsFyhB6dccRCvrj6WsbizsF95u0gA
π» Code
π Actual behavior
display an error message:
'context' is of type 'unknown'.
'context' is of type 'unknown'.
'T4' is declared but its value is never read.
'data' is declared but its value is never read.
π Expected behavior
The type of 'context' should already be determined; it shouldn't be 'unknown'.
Additional information about the issue
No response