Since TypeScript 3.5 started to warn on possibly unsafe index access writes, this has been a pain point. In TypeScript 3.4 and below this would have been acceptable code since it would have been interpreting as assigning string | number
to string | number
; the downside is that it would also have been acceptable to write d[key] = Math.random() < 0.5 ? "str" : 123;
, which is clearly wrong.
In TypeScript 3.5 and above the compiler is now overly strict in cases like this; it insists that you can only assign something which is both a string
and a number
to d[key]
since it isn't sure which element of keyof X
the key
is. Since there are no values of type string & number
, the type is reduced to never
. So it's impossible to safely assign any value to d[key]
according to the compiler.
There's an open issue, microsoft/TypeScript#32693, asking to allow such assignments, in the case where the key is the exactly the same in both the read and the write, and not just the same type (since two values of type keyof X
could be different keys, but two references to the same value of type keyof X
cannot). This hasn't been implemented yet, though. If you want to see it implemented, you might want to go to that issue and give it a 👍. In the meantime, we have to work around it.
One workaround here (other than using a type assertion which is always an option, or writing redundant code as in the other answer) is to make your callback function generic in the type of key
:
ke.forEach(<K extends keyof X>(key: K) => {
let nn = d[key];
nn = one[key];
d[key] = nn;
})
There is no error there because the compiler does allow reads and writes to values of identical types, and when you have K
as an unresolved generic, the compiler thinks that X[K]
on the read is the same as the X[K]
on the write. (I got this from this comment, which is explained by the chief architect of TS).
Anyway, hope that helps; good luck!
Playground link to code