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
type MapOf<T>{[x: string ]: T|never}// values only of type TtypeBox<T>{value: T}// Here we show that the above type constrains valid types for values of MapOf<T> to Tconstt: MapOf<number>={foo: 1}// compilesconsttbad: MapOf<number>={foo: 'foo'}// does not compile// assign every value of MapOf<T> to some TfunctionforMapsOf<T>(m: MapOf<T>,t: T){for(varkinm){m[k]=t}}// Compiles and works as expected// now let's transform the definition of the above function slightlyfunctionforMapsOf<T,MextendsMapOf<T>>(m: M,t: T){for(varkinm){m[k]=t}}// DOES NOT COMPILE - Says "Type T is not assignable to type M[keyof M]// The above is crucial for this function below which would take a map of // Boxed values and update them from a map of unboxed valuesfunctionupdateBoxed<T,UextendsOnly<T>,Bextends{[KinkeyofU]: Box<U[K]>}>(b: B,u: U){for(varkinu){b[k].value=u[k]}}// DOES NOT COMPILE - Says "Type U[keyof U] is not assignable to type T"
Expected behavior:
I would expect the value of a type indexing itself to in-fact be all the types of its values. This should be knowable at compile-time and especially so if the type is constrained as T | never
Actual behavior:
Compiler does not recognize that U[keyof U] is the same as T.
I am not not sure i understand the issue. so bear with me..
I do not see how the maps is related tot he boxed version. Mapof<T> forgets all the properties by vertue of the indexer. so it does not fit with the updateBoxed function. i would expect this to be typed as:
Activity
mhegazy commentedon Nov 26, 2016
I am not not sure i understand the issue. so bear with me..
I do not see how the maps is related tot he boxed version.
Mapof<T>forgets all the properties by vertue of the indexer. so it does not fit with theupdateBoxedfunction. i would expect this to be typed as:ahejlsberg commentedon Nov 28, 2016
Steven (@stevekane) Check out #12528 for an example of how to implement this.