I am currently developing a type-safe object mapper in TypeScript.
So far, I have successfully implemented this at a single level of depth using this playground.
enum TransformerActions {
Delete = 'delete',
}
type TransformerMap<S> = {
[P in keyof S]?: S[P] extends object
? ((s: S) => any) | TransformerActions
: S[P];
};
interface Name {
forename?: string;
surname?: string;
}
type Person = { currentName: Name, previousNames: Name[] }
const personTransformer1: TransformerMap<Person> = {
currentName: TransformerActions.Delete
}
const personTransformer2: TransformerMap<Person> = {
currentName: (s) => s.currentName.forename + ' ' + s.currentName.surname
}
However, when attempting to extend this functionality to support recursive types with nested key transformations, I encountered an issue with the syntax. I tried the following approach:
type TransformerMap<S> = {
[P in keyof S]?: S[P] extends object
? TransformerMap<S[P]>
: S[P] | ((s: S) => any) | TransformerActions;
};
Unfortunately, this solution did not work as expected.
I am now seeking guidance on how to create a recursive type in this manner.