Cannot map over intersection of string
with string literal (e.g., "a" & string
)
#23651
Labels
Milestone
string
with string literal (e.g., "a" & string
)
#23651
TypeScript Version: 2.9.0-dev.20180424 but reproducible as far back as I tested (2.7)
Search Terms: intersection, mapped, empty, string, literal
Code
Expected behavior:
X
should be equivalent to{ a: 1 }
.Actual behavior:
X
is{}
.Playground Link: link
Related Issues:
maybe #12114 for mapped types in general
maybe #23592 for making me notice this
maybe #16386 because I always think everything would be fixed by absorption
maybe #9410 although back then these things were treated as
never
everywhereThe recent #23592 change allowing mapping over
number
andsymbol
keys caused some of my existing code to break, and the obvious fix for some of them was to restrict those instances where I really expectedstring
s fromkeyof T
to(keyof T) & string
. But that doesn't work. It looks like"a" & string
gets treated likenever
inside of a mapped type key set, but like"a"
everywhere else. Absorption would fix this, but barring that, maybe some less aggressive simplification inside mapped types?Workaround: Since TS2.8 it is possible to use
Extract<T, U>
instead ofT & U
in some instances. In this case it can reduce the intersection before the mapping:type X = { [K in Extract<'a',string>]: 1 }
is equivalent to{ a: 1 }
as expected.The text was updated successfully, but these errors were encountered: