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
typeArgMap={a: number,b: string};typeFunc<KextendskeyofArgMap>=(x: ArgMap[K])=>void;typeFuncs={[KinkeyofArgMap]: Func<K>};constfuncs={a: (x: number)=>{},b: (x: string)=>{},};functionf1<KextendskeyofArgMap>(key: K,arg: ArgMap[K]){funcs[key](arg);}functionf2<KextendskeyofArgMap>(key: K,arg: ArgMap[K]){constfunc=funcs[key];// Type Funcs[K]func(arg);}functionf3<KextendskeyofArgMap>(key: K,arg: ArgMap[K]){constfunc: Func<K>=funcs[key];// Error, Funcs[K] not assignable to Func<K>func(arg);}
π Actual behavior
Unexpected error in example above.
It's unexpected because throwing away type information makes this type check correctly, by widening with either const funcs: Funcs (all examples) or const func: Funcs[K] (in f2). So the compiler actually is able to confirm that indexed type is assignable to the mapped type, it just doesn't try without being explicitly prompted.
π Expected behavior
No errors in example above.
The text was updated successfully, but these errors were encountered:
K can also be a union type, like "a" | "b". In this case your type Funcs[K] resolves to (x: number) => void | (x: string) => void. When trying to call a union like this, you must provide a value that's compatible with all possible argument types, because the compiler doesn't know whether it has a function accepting a string or a number - so the type you must provide must be both: string & number. But such a type can't be represented, so you end up with never.
Bug Report
@ahejlsberg This issue seems nearly identical to #47368, but this time for const object types which are assignable to the mapped type.
π Search Terms
const indexed access; object literal indexed access; object literal widen; widen mapped type
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
Unexpected error in example above.
It's unexpected because throwing away type information makes this type check correctly, by widening with either
const funcs: Funcs
(all examples) orconst func: Funcs[K]
(in f2). So the compiler actually is able to confirm that indexed type is assignable to the mapped type, it just doesn't try without being explicitly prompted.π Expected behavior
No errors in example above.
The text was updated successfully, but these errors were encountered: