-
Notifications
You must be signed in to change notification settings - Fork 118
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
TS2322: Type '{}' is not assignable to type 'DeepPartial<T>' #123
Comments
Confirmed, thanks for finding this! Looks like this was introduced at v3.0, when the @krzkaczor @FrancescoBorzi can you see which of the new conditions cause |
The last version (2.12) that worked with export type DeepPartial<T> = {
[P in keyof T]?: T[P] extends Primitive
? T[P]
: T[P] extends Function
? T[P]
: T[P] extends Date
? T[P]
: T[P] extends Array<infer U>
? Array<DeepPartial<U>>
: T[P] extends ReadonlyArray<infer U>
? ReadonlyArray<DeepPartial<U>>
: DeepPartial<T[P]>
}; |
I think there might be some bug related to TypeScript's recursion mechanism when there are conditions. See the following example: export type DeepPartial<T> = T extends Builtin
? { [K in keyof T]?: DeepPartial<T[K]> }
: { [K in keyof T]?: DeepPartial<T[K]> };
const myFunction = <T extends {}>(bool: boolean, obj: T): DeepPartial<T> => bool ? {} : obj;
// Type '{}' is not assignable to type 'DeepPartial<T>'.ts(2322) While export type DeepPartial<T> = { [K in keyof T]?: DeepPartial<T[K]> };
const myFunction = <T extends {}>(bool: boolean, obj: T): DeepPartial<T> => bool ? {} : obj; |
Should we file an issue to TS then? Can we somehow work around this issue? (well, apart from going back to the form before the refactor I linked) |
The side effect of conditional types, the issue to TypeScript - microsoft/TypeScript#37524 |
Using
ts-essentials
version5.0.0
.I have a function with the following types:
I know this is a bit weird but this is just to have a minimal bug reproduction: basically my function can return
T
, aDeepPartial<T>
or the empty object{}
.Unfortunately, I get this error:
However, if I use
Partial<T extends {}>
I don't get this issue:so basically
{}
can be aPartial<T>
, but it can't be aDeepPartial<T>
, which is weird.The text was updated successfully, but these errors were encountered: