Take a look at this code snippet:
const arr = [1,2,3]
const res1 = arr.slice()
const res2 = Object.assign([],arr)
When using arr.slice()
to create a shallow clone, the resulting array res1
will have a type of number[]
, just like the original arr
. However, if you use Object.assign()
instead, the array res2
will have a type of never[] & number[]
.
But why does the type of res2
include never[]
? How can it be both number[]
and never[]
at the same time (using the &
operator)?