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
Seems that indexer type could be better recognized in for loop. Please check the code. TypeScript Version: 3.7.x-dev.201xxxxx
Search Terms:
mapped type, keyof Code
typeSimpleType={a: string,b: number}letx: SimpleType={a: "",b: 9}letfields: (keyofSimpleType)[]=["a"]for(constfoffields){x[f]=x[f]// This throws TS 2322, although the type is obviously the same}
Expected behavior:
The type of x[f] is the same as the x[f] and should be assignable. Actual behavior:
x[f] is not assignable to x[f] Playground Link: example
Related Issues:
Actually the initial issue was with mapped type, but while looking for the cause and workaround I created this simple showcase. Suppose fixing this would resolve mapped type issue.
In general this is a correct error. You couldn't validly, for example, write this (it would sometimes corrupt the object), even though the types are the exactly the same:
Your code makes sense, but it is a different case from the one I submitted.
You proposed general case and x[g] is not assignable to x[f]. However its a bit tricky and special case should be considered. So, if the indexer is the same type than operation should be allowed.
The type of indexer is not just its declared type, perhaps it also depends on exact instance. This is not currently checked with compiler, and this raise error for correct code , please check the code.
typeSimpleType={a: string,b: number}letx,y: SimpleType={a: "",b: 9}letfields: (keyofSimpleType)[]=["a","b"]for(constgoffields){for(constfoffields){x[f]=x[g]//OK (Error -> and should not be allowed; type are not same: each one is string|number )y[f]=x[f]//Error -> but is OK (types are the same: each one is string or each one is number)x[f]=x[f]//Error -> but is OK (types are the same)leth=(Math.random()>0.5) ? fields[0] : fields[1]y[h]=x[x]//Should be an errorletj=fy[j]=x[f]//Would be nice if the complier let this compile to}}
Activity
RyanCavanaugh commentedon Mar 27, 2020
In general this is a correct error. You couldn't validly, for example, write this (it would sometimes corrupt the object), even though the types are the exactly the same:
See #32693
typescript-bot commentedon Mar 29, 2020
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
k4hvd1 commentedon Apr 1, 2020
Your code makes sense, but it is a different case from the one I submitted.
You proposed general case and x[g] is not assignable to x[f]. However its a bit tricky and special case should be considered. So, if the indexer is the same type than operation should be allowed.
The type of indexer is not just its declared type, perhaps it also depends on exact instance. This is not currently checked with compiler, and this raise error for correct code , please check the code.
code link
Proposal:
add additional check while compiling that checks both type and variable in indexer
add special keyword (to simplify and limit compiling effort)
and implement additional indexer instance check
and implement additional indexer instance check
TiagoCavalcante commentedon Nov 27, 2022
Any updates on this?
This feature would be really useful and doesn't seems to be as complicated as other new TypeScript features