You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeFoo={[Tinstring]: Textends'a' ? number : string;}letfoo: Foo={a: 1,b: 'c'}asconst;foo['a'as'a']=2asconst;foo['a'asconst]=2asconst;
Expected behavior:
The code should not emit errors. TS should not expand the type of 'a' to string for the purpose of using this type in a conditional with the extends 'a' condition.
Actual behavior:
The attached code errors in all last 3 lines. It seems no matter how hard I try to tell the compiler I don't want to expand the type of 'a' into string it still does it.
By the time you get those errors Foo will have already been resolved as { [x: string]: string }. The behavior of mapped types over string is to just make an index signature with string and instantiate the retrun type of the index with the key type parameter (T in this case) as string. So for example:
typeFoo={[Tinstring]: T[]}/// just a fancy way of saying { [x: string]: string[]; }
I think Negated types (#29317) and the possibility to specify any valid key type in an index signature (#26797) will enable you to construct this type.
TypeScript Version: 3.4.0-dev.20190316
Search Terms:
Code
Expected behavior:
The code should not emit errors. TS should not expand the type of
'a'
tostring
for the purpose of using this type in a conditional with theextends 'a'
condition.Actual behavior:
The attached code errors in all last 3 lines. It seems no matter how hard I try to tell the compiler I don't want to expand the type of
'a'
intostring
it still does it.Playground Link:
The above example uses TS 3.4 features so Playground won't work. But here's a version without
as const
that's parseable on the playground:https://www.typescriptlang.org/play/#src=type%20Foo%20%3D%20%7B%0D%0A%20%20%20%20%5BT%20in%20string%5D%3A%20T%20extends%20'a'%20%3F%20number%20%3A%20string%3B%0D%0A%7D%0D%0Alet%20foo%3A%20Foo%20%3D%20%7B%20a%3A%201%2C%20b%3A%20'c'%20%7D%3B%0D%0Afoo%5B'a'%20as%20'a'%5D%20%3D%202%3B%0D%0A
Related Issues:
extends '1'
but it does it in the context of generics which my issue doesn't use.The text was updated successfully, but these errors were encountered: