I'm trying to create a function createCells
that takes an array of strings and returns an object whose properties depends on the index of items in the array
For example the function createCells(['10', '12', '13'])
would return an object with properties {Cell0, Cell1, Cell2}
So far I've been able to create one where the object returned is based on the value of items in the array
const createCells = <T extends `${number}`[]>(
args: T,
) => {
return args.reduce(
(prev, curr) => ({
...prev,
[`Cell${curr}`]: () => { },
}),
{}
) as {
[key in typeof args[number] as `Cell${key}`]: (
) => void;
};
};
but this way the function createCells(['10', '12', '13'])
would return an object with properties {Cell10, Cell12, Cell13}
For what I'm trying to achieve, the function would be
const createCells = <T extends `${number}`[]>(
args: T,
) => {
return args.reduce(
(prev, curr, index) => ({
...prev,
[`Cell${index}`]: () => { },
}),
{}
)
};
How do i use typescript to indicate the type of the object returned by the function