Home > Enterprise >  Typescript: Create recursive mapped type that maps from existing type to string
Typescript: Create recursive mapped type that maps from existing type to string

Time:11-30

Given a type how can I write a recursive mapped type that yields a type that is with all the same keys but with their types being strings instead of whatever their incoming type is? Specifically, I want to handle nested objects & arrays.

type MySourceType = {
  field1 :string,
  field2: number,
  field3: number[],
  field4: Date, 
  field5: {
    nestedField1: number,
    nestedField2: number[]
    nestedField3: Date,
  }
}
type MyDestinationType = MakeAllFieldsString<MySourceType>;

should yield:

type MyDestinationType = {
    field1 :string,
    field2: string,
    field3: string[],
    field4: string, 
    field5: {
      nestedField1: string,
      nestedField2: string[]
      nestedField3: string,
    }
  }

this works for a regular "flat" object but fails to handle the nested objects and arrays

type JsonObject<T> = {[Key in keyof T]: string; }

I also tried this but it didn't seem work do what I expected either.

type NestedJsonObject<T> = {
[Key in keyof T]: typeof T[Key] extends object ? JsonObject<T[Key]> : string;
}

CodePudding user response:

You could do like this:

type MySourceType = {
  field1: string;
  field2: number;
  field3: number[];
  field4: Date;
  field5: {
    nestedField1: number;
    nestedField2: number[];
    nestedField3: Date;
  };
};

type Literal = { [k: string]: {} };

type JsonObject<T> = T extends Array<infer U>
  ? U extends Literal
  ? Array<JsonObject<U>>
  : Array<string>
  : T extends Literal
  ? { [Key in keyof T]: JsonObject<T[Key]> }
  : string;

type MyDestinationType = JsonObject<MySourceType>;

TypeScript playground: https://tsplay.dev/WYBlgw

  • Related