In my opinion, TypeScript interprets the following code snippet:
let partOfSomeNums2 = [someNums2[1], someNums2[0]];
as
let partOfSomeNums2 = [0, 1];
The second line defines partOfSomeNums2
as a numeric array, just like the first line.
An alternative approach would be:
let partOfSomeNums2 = [...someNums2.slice(1, 2), ...someNums2.slice(0, 1)];
Or even:
let partOfSomeNums2: (0 | 1 | 2)[] = [someNums2[1], someNums2[0]];
I believe your someWorkingFunction
in your playground is functional because the type 0 | 1 | 2
is derived directly from an enum, unlike your non-functional code where the value 0
is hardcoded.
UPDATE
If it were up to me, I would opt for this solution: Playground
To implement this strategy: type SmallNumber = 0 | 1 | 2;
Subsequently, employ this type in your mapping function:
const someNums2 = someObjects2.map((obj): SmallNumber =>
typeof obj.num === "undefined" ? 0 : obj.num
);