0

type or interface could both work.

interface example {
  s: string;
  o: {
    t: string;
    arrn: number[];
    arr: {
      u: string;
    }[];
};

Would transform to

interface transformed {
  s: {
    onChange: (v: string) => void;
  };
  o: {
    t: {
      onChange: (v: string) => void;
    };
    arrn: {
      onChange: (v: number) => void;
    }[];
    arr: {
      u: {
        onChange: (v: number) => void;
      };
    }[];
};

Is this possible?

What is the direction to do something like this?

2 Answers 2

1

I think the following type satisfies what you describe:

interface example {
  s: string;
  o: {
    t: string;
    arrn: number[];
    arr: {
      u: string;
    }[];
  };
}

type ReplacePrimitives<T> = T extends Record<any, any>
  ? { [K in keyof T]: ReplacePrimitives<T[K]> }
  : { onChange: (value: T) => void };

type transformed = ReplacePrimitives<example>;

It recursively replaces all primitive values with the onChange object you describe, leaving records and array structures in place.

Sign up to request clarification or add additional context in comments.

2 Comments

Haha yeah, wish there was some indication on SO like "someone is currently answering this" :)
Oh good this was easier than I expected! I forgot arrays and objects were both records. Thank you - I can't assign 2 answer correct :P
1
type Transform<T> = T extends object
  // T is an object (including an array), so transform each property
  ? {[K in keyof T]: Transform<T[K]>}
  // T is a primitive, so do the transformation
  : {onchange: (v: T) => void};

type transformed = Transform<example>

This works as expected for tuples and arrays as well:

// {onchange: (v: string) => void}[]
type ArrayTest = Transform<string[]>;
// readonly {onchange: (v: string) => void}[]
type ReadonlyArrayTest = Transform<readonly string[]>;
/* [
  {onchange: (v: string) => void},
  {onchange: (v: number) => void},
  ...{onchange: (v: boolean) => void}[],
] */
type TupleTest = Transform<[string, number, ...boolean[]]>;

Playground link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.