Summarized
Writing produces type intersection in TS.
Detailed
This is a copy of the answer from the official repo microsoft/TypeScript#47295, this is a functionality that “work as intended”.
In most of your setups, the type of
prop
is not narrowed down from"num" | "str" | "arr"
; since you’re writing totarget[prop]
rather than reading from it, that produces an intersection (not a union) of all the possible types it could refer to. If there’s no overlap, the intersection collapses tonever
, because there’s no possible value that works for all of them.
This answer is created to close the question.
CLICK HERE to find out more related problems solutions.