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
Generic type inference prefers conditional types #31766
Expected behavior:
No errors. A trivial type inference is T=I. This is also the behavior of TypeScript 3.3.
Actual behavior: error TS2322: Type 'Partial<I>' is not assignable to type 'I'.
From that error message the actual inference is T=Partial<I> (or Cond<I>), which seems like a regression to me.
Related Issues:
There are some issues around conditional types but nothing quite a duplication, I think. #31601 requires function overloads #24085 is a problem with enums #30489 is more about type inference on the conditional type itself, not the generic type when the conditional type is used #30341 is similar but it's marked as fixed
To create a conditional type. I think the problem only happens with a conditional type.
If the example is oversimplified and doesn't make sense, below is another example, without the T extends any.
interfaceI{a: string}typeDeepPartial<T>=Textendsobject ? {[KinkeyofT]?: DeepPartial<T[K]>} : T;declarefunctionf<T>(t: T,partial: DeepPartial<T>): T;functiong(p1: I,p2: Partial<I>): I{returnf(p1,p2);// error Type 'Partial<I>' is not assignable to type 'I'.}
interfaceI{a: string}typeDeepPartial<T>=Textendsobject ? {[KinkeyofT]?: DeepPartial<T[K]>} : T;declarefunctionf<T>(t: T,partial: DeepPartial<T>): T;functiong(p1: I,p2: Partial<I>): I{// T inferred to be Partial<I>; should be Ireturnf(p1,p2);// error Type 'Partial<I>' is not assignable to type 'I'.}
This is caused by #30010. Part of why we inferred to a union type was to get lower priority candidates for inferences to naked type variables, but that got lost with the fix in #30010. We just need to reinstate that part.
To be fair, at the time I'm pretty sure we had done away with the naked type variable priority; we just didn't think to add it to the conditional inference bit when we added it back. :P
Activity
RyanCavanaugh commentedon Jun 13, 2019
What is the intent of writing
T extends any?bowenni commentedon Jun 13, 2019
To create a conditional type. I think the problem only happens with a conditional type.
If the example is oversimplified and doesn't make sense, below is another example, without the
T extends any.RyanCavanaugh commentedon Jun 25, 2019
ahejlsberg commentedon Jul 1, 2019
This is caused by #30010. Part of why we inferred to a union type was to get lower priority candidates for inferences to naked type variables, but that got lost with the fix in #30010. We just need to reinstate that part.
weswigham commentedon Jul 2, 2019
To be fair, at the time I'm pretty sure we had done away with the naked type variable priority; we just didn't think to add it to the conditional inference bit when we added it back. :P