'what does this typescript function declaration with generics mean in trying to split an array into evens and odds

I have to write a function in Typescript to split the array into two arrays of evens and odds.

function evenAndOddArr<T>(arr: T[]): [T[], T[]] {...}

I am slightly confused as to what <T>(arr: T[]): [T[], T[]] this segment is referring to. I get that I am working with generics and I know how to split an array into evens and odds easily enough. What I don't understand specifically is how to return those two arrays back using T[] and T[].

Am I correct in assuming the two T[]s are the generic arrays that will be returned once properly split into evens and odds? If so, how would I call to them in the function itself?



Solution 1:[1]

Maybe this verbose approach makes it more clear:

const evenAndOddArray<Item>(array: Array<Item>): [Array<Item>, Array<Item>] => // ...

Still, being a function that splits your array in a tuple of odds and evens, is better to just assume it will be numbers:

const evenAndOddArray(array: Array<number>): [Array<number>, Array<number>] => // ...
// or
const evenAndOddArray(array: number[]): [number[], number[]] => // ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman