Can you analyze this typescript code snippet for me?
type params1 = {p:number} ;
type params2 = {p:boolean};
type params3 = {p:string};
type res = {r:string};
const fObject = {
a:(a:params1):res=>{return {r:`${a.p}`}},
b:(a:params2):res=>{return {r:`${a.p?1:0}`}},
c:(a:params3):res=>{return {r:a.p}}
}
Is it possible to define a type similar to the following?
type params = params1 | params2 | params3
I am planning to create a new object that includes all the functions from fObject
, while also implementing a logger function on the result of each function.
It would look something like this:
const loggingObject: typeof fObject = Object.keys(fObject).reduce(
(result: any, key: string) => {
result[key] = (args: any/*reason for Q*/) => logger.report(fObject
[key as loggingObject](args /*If i put something like 2 it says it has no properties in common with params1 & param2 & param3, thats why I know its possible*/));
return result;
},
{},
);
I want to avoid using the any type and instead require a dynamic type that can accept an object with functions and determine the union of parameter types of all the functions within the object