I have created a Factory function where it takes an object as input and if that object contains specific properties, the factory transforms those properties into methods.
How can I utilize mapped Types to accurately represent the type of the resulting object?
For example, let's assume the convertible properties are named foo, bar, baz:
interface IFactoryConfig {
foo?: string;
bar?: string;
baz?: string;
}
And the transformed properties should be:
interface IFactoryResult {
foo(someParam: string): boolean;
bar(): number;
baz(otherParam: number): void;
}
If the initial object's type is
interface IInputObject {
baz: string;
notPredefined: string;
aNumber: number;
foo: string;
aMethod(): void;
}
The factory replaces baz and foo with methods and outputs:
interface IInputObject {
baz(otherParam: number): void;
notPredefined: string;
aNumber: number;
foo(someParam: string): boolean;
aMethod(): void;
}
I am attempting to incorporate mapped types for property replacement:
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>;
interface IFactory {
<InputType extends IFactoryConfig, ResultType>(config: InputType): Omit<InputType, keyof IFactoryConfig> & Pick<IFactoryResult, ?>;
}
I am unsure of what should go within the Pick<> to select properties from IFactoryResult that also exist in InputType.