-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Conditional types fail to distribute in properties of mapped types #33669
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
Comments
This issue is actually a duplicate of #22945!. The aforementioned issue can be worked around similarly (using type Id<T> = T;
type Z<T> = Id<T> extends true ? Id<T> : never;
type z = Z<boolean>;
type A<T extends any[]> = Z<T[0]>;
type a = A<[boolean]>;
type B<T extends {}> = { [P in keyof T]: Z<T[P]> }[keyof T];
type b = B<{a: boolean}>; I'm not sure whether this issue should be closed as a result, but I still believe this is unexpected behaviour - that an "auxiliary" type is needed to write the types as intended, when type aliases should be equivalent to writing the expansion inline. |
Another way of working around this is to use type NullifyStringsInPropsWorking2<T> = {
[K in keyof T]:
T[K] extends infer P
? P extends string ? null : P
: never
}
type TestType = { a: number | string }
// { a: number | null }
type WorkingReplaceProps2 = NullifyStringsInPropsWorking2<TestType> This avoids the need for an auxiliary type. |
Duplicate of #23022, #23046, and #32274. Regarding:
the subtext is that this only applies when inlining a type alias instantiated with a type parameter. Did you see this section of the handbook: distributive-conditional-types? I don't mean this in a snide way --- lots of people have been tripped up on this and if they are reading the handbook and still getting confused then I think that is good to know. |
I didn't, actually! After reading that section of the handbook, I think I still wouldn't have understood it - the section focuses on some nice working examples and doesn't show any examples for when things go wrong. IMO these things would be nice to have in the handbook for better understanding:
|
You would be surprised! 😅 |
Conditional types fail to distribute in properties of mapped types as described in microsoft/TypeScript#33669 #issuecomment-536493169. This uses the infer approach to avoid needing an auxiliary type to fix the issue.
TypeScript Version: 3.7.0-dev.20190928
Search Terms: conditional mapped property union
Code
Expected behavior:
NullifyStringsInPropsWorking
andNullifyStringsInPropsBroken
should be functionally identical - expanding theNullifyStrings
type alias inNullifyStringsInPropsWorking
results in the same definition asNullifyStringsInPropsBroken
.Actual behavior:
NullifyStringsInPropsWorking
andNullifyStringsInPropsBroken
have different behaviour -BrokenReplaceProps
has type{ a: string | number }
instead of the expected{ a: number | null }
.Playground Link: Link
Related Issues:
#28339, but seems to be different.
#22945 mentions
but that is not the case here.
The text was updated successfully, but these errors were encountered: