2021-11-30

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

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;
}


from Recent Questions - Stack Overflow https://ift.tt/3I7q1HR
https://ift.tt/eA8V8J

No comments:

Post a Comment