I have developed a TypeScript module that simplifies the process. This function takes an object as input and executes all the functions while updating the values of the corresponding properties.
If any property in the object is not a function, it will be ignored during the process.
Below is the complete functional source code implemented in vanilla JavaScript, which needs to be converted into TypeScript:
import _ from 'lodash'
export default function flexifyParams(params) {
const composedObject = {}
for (const [key, value] of Object.entries(params)) {
composedObject[key] = _.isFunction(value) ? value() : value
}
return composedObject
}
Here's an example showcasing how this function operates:
const test = {
x: () => 'hi',
y: () => 123,
z: () => ['a', 'b', 'c'],
a: 'my string',
}
const P = flexifyParams(test)
// The output for P would be:
// {
// x: 'hi',
// y: 123,
// z: ['a', 'b', 'c'],
// a: 'my string',
// }
I attempted to migrate this function to TypeScript by updating the code as follows:
import _ from 'lodash'
export default function flexifyParams<PARAMS extends object>(params: PARAMS): PARAMS {
const composedObject: { [index: string]: string | Symbol | number } = {}
for (const [key, value] of Object.entries(params)) {
composedObject[key] = _.isFunction(value) ? value() : value
}
return composedObject as PARAMS
}
The above code works fine, but the function's return type needs adjustment.
While the keys of the returned object match those from the input parameter, their values are not identical; they are results from the respective functions.
How can I accurately define the return type for this function using TypeScript?