I can define and use a generic function like this:
const fetchData = <T>(): T[] => {
const arr: T[] = []
// if stuff push to arr
return arr
}
const emptyStringArray = fetchData<string>();
However, in my actual scenario, I have a lot of params and would like to separate out the typings and function assignment.
I've attempted to write like this:
type IFetchData = <T>() => T[]
const fetchData2: IFetchData = () => {
const arr: T[] = []
// if stuff push to arr
return arr
}
const emptyStringArray = fetchData2<string>();
However, now the function definition doesn't recognize the T
as an available type.
Cannot find name 'T'.
I've tried a lot of different configurations on where to put the <T>
, but nothing seems to work - any ideas?