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
{{ message }}
Repository navigation
Generic mapped type constrained by a type parameter has different members / index signatures when constraint of that type parameter is a union #27819
Search Terms: mapped type parameter member index signature constraint constrained union
Code (with noImplicitAny)
functionfoo1<Kextendsstring>(arg: {[PinK]: string}){// Actual: Error: Element implicitly has an 'any' type because type '{ [P in K]: string; }' has no index signature.returnarg["test"];}functionfoo2<Kextendsstring|number>(arg: {[PinK]: string}){// Actual: No errorreturnarg["test"];}functionfoo3<Kextends"foo">(arg: {[PinK]: string}){// Actual: Error: Property 'foo' does not exist on type '{ [P in K]: string; }'.returnarg.foo;}functionfoo4<Kextends"foo"|"bar">(arg: {[PinK]: string}){// Actual: No errorreturnarg.foo;}
Expected behavior: Consistent behavior between foo1 and foo2 and between foo3 and foo4.
Looks like the cause is that this line in resolvedMappedTypeMembers in the checker is using getApparentType, which changes either string or "foo" to String (which doesn't generate an index signature) but leaves unions alone. What was the thinking here??
Activity
ahejlsberg commentedon Feb 5, 2019
I believe this was fixed by #29121. All four examples now produce errors as expected.