-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed as not planned
Closed as not planned
Copy link
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
π Search Terms
mapped conditional type keyof generic extends ternary
π Version & Regression Information
- This changed between versions 4.0.5 and 4.1.5
(Because the mapped type wouldn't work prior to 4.1)
β― Playground Link
π» Code
export type MappedToTraversed<T> =
{
[K in keyof T as T[K] extends any ? K : K]: 'traversed'
}
type HoverThis1 = MappedToTraversed<{
anyProp: any
strProp: string
objProp: object
}>
// βοΈ all properties are now "traversed":
// type HoverThis1 = {
// anyProp: "traversed";
// strProp: "traversed";
// objProp: "traversed";
// }
type HoverThis2<T> = MappedToTraversed<{
anyProp: any
strProp: string
objProp: T
}>
// β objProp is missing:
// type HoverThis2<T> = {
// anyProp: "traversed";
// strProp: "traversed";
// }
type HoverThis3<T> = MappedToTraversed<{
anyProp: any
strProp: string
objProp: Record<keyof T, number>
}>
// β objProp is missing:
// type HoverThis3<T> = {
// anyProp: "traversed";
// strProp: "traversed";
// }
π Actual behavior
The objProp
is missing from the HoverThis
types when it is somehow using a generic (e.g. when used as property type or key in a Record
). Looks like it is not being traversed during mapping at all due to the extends clause, because the ternary as T[K] extends any ? K : K
should IMHO always evaluate to K
.
π Expected behavior
objProp
should be listed in all HoverThis
types.
Additional information about the issue
I think it's due to the extends
clause, because if the mapping is simply written as
export type MappedToTraversed<T> =
{
[K in keyof T as K]: 'traversed'
}
objProp
is listed in the resulting HoverThis
types.
It also fails if
- the ternary is the other way round, i.e.
any extends T[K] ? K : K
- the ternary checks something that should always be true
T[K] extends T[K] ? K : K
- I try to disable union distribution according to hand book:
[T[K]] extends [any] ? K : K
However, if I mess up disabling the union distribution (putting only one side in square brackets), it works..? [T[K]] extends any ? K : K
. This feels like an unsafe workaround.
jcalz
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed