Hello 👋
I'm struggling with a typing a function and I've got myself into a bit of a mess trying to figure it out.
Ignore the reproduction example, there's much more at play, but I've narrowed it down to the core functionality that I'm struggling to type.
JS example (Full TS attempt below):
const createLambdaParams = ({ genericParams, fnNames }) => {
const lambdaParams = fnNames.reduce((acc, fnName) => {
acc[`${fnName}Params`] = { ...genericParams, functionName: `${fnName}Lambda` };
return acc;
}, {});
return lambdaParams;
}
The function works as I would expect:
createLambdaParams({genericParams, fnNames: ["fnOne", "fnTwo", "fnThree"]})
Returning an object that looks like this:
{
"fnOneParams": {
"region": "eu-central-1",
"bucket": "my-bucket",
"functionName": "fnOneLambda"
},
"fnTwoParams": {
"region": "eu-central-1",
"bucket": "my-bucket",
"functionName": "fnTwoLambda"
},
"fnThreeParams": {
"region": "eu-central-1",
"bucket": "my-bucket",
"functionName": "fnThreeLambda"
}
}
There are a few things I'm trying to achieve, but I'm really struggling as far as typing the return keys.
- Type completetion on the object being passed into the params
- Type completion on the destructuring of the return value
- Not casting the return type to the reduce object {}
Below is where I'm currently at with typing the function, along with a tsplayground link - It's in a bit of a mess right now through the trial and error - Any help would be appreciated!