Open
Description
TypeScript Version: 3.7.0-dev.20190928
Search Terms: conditional mapped property union
Code
type NullifyStrings<T> = T extends string ? null : T
type NullifyStringsInPropsWorking<T> = { [K in keyof T]: NullifyStrings<T[K]> }
type NullifyStringsInPropsBroken<T> = { [K in keyof T]: T[K] extends string ? null : T[K] }
type TestType = { a: number | string }
// { a: number | null }
type WorkingReplaceProps = NullifyStringsInPropsWorking<TestType>
// { a: string | number }
type BrokenReplaceProps = NullifyStringsInPropsBroken<TestType>
Expected behavior:
NullifyStringsInPropsWorking
and NullifyStringsInPropsBroken
should be functionally identical - expanding the NullifyStrings
type alias in NullifyStringsInPropsWorking
results in the same definition as NullifyStringsInPropsBroken
.
Actual behavior:
NullifyStringsInPropsWorking
and NullifyStringsInPropsBroken
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
Since type aliases are equivalent to writing the expansion inline [...]
but that is not the case here.