-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generic type inference prefers conditional types #31766
Comments
What is the intent of writing |
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 interface I { a: string }
type DeepPartial<T> =
T extends object ? {[K in keyof T]?: DeepPartial<T[K]>} : T;
declare function f<T>(t: T, partial: DeepPartial<T>): T;
function g(p1: I, p2: Partial<I>): I {
return f(p1, p2); // error Type 'Partial<I>' is not assignable to type 'I'.
} |
interface I { a: string }
type DeepPartial<T> =
T extends object ? {[K in keyof T]?: DeepPartial<T[K]>} : T;
declare function f<T>(t: T, partial: DeepPartial<T>): T;
function g(p1: I, p2: Partial<I>): I {
// T inferred to be Partial<I>; should be I
return f(p1, p2); // error Type 'Partial<I>' is not assignable to type 'I'.
} |
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 |
TypeScript Version: 3.4.5 and 3.5.1
Search Terms: Conditional types, Type inference
Code
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>
(orCond<I>
), which seems like a regression to me.Playground Link:
playground link
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
@rkirov
The text was updated successfully, but these errors were encountered: