如何将打字稿中的递归类型分配到深层嵌套的对象中?

问题描述 投票:0回答:0

我试过这样做,但它不会给我建议,也不会让我分配新的值:

  type UpdateValues<SomeObj extends Record<string, unknown>, NewValues extends RecursivePartial<SomeObj>> = {
    [K in keyof SomeObj]: K extends keyof NewValues ? NewValues[K] : SomeObj[K];
  };
  type RecursivePartial<T> = {
    [P in keyof T]?: T[P] extends Record<string, unknown> ? RecursivePartial<Partial<T[P]>> : T[P];
  };

type Person = {
  area?: "south" | "east",
  age: number  , 
  name: string 
   ,  family?: {
    brother?: Person[] , sisters?: Person[] , parents?:  {
      mom?: Person,
      dad?: Person,
    }
  }
}


type AdamFamily = UpdateValues<  Person , {
 area:  "west", //!!!!! here when i try to assign new value to the "area"
  name: "john" | "Dave" | "Mike" , family: {
    parents:{
      mom: {
        name: "Adva" , age: 45 , area :"" //!!!!! it wont gives the option to choose from  like it want me to guess the currect string
      },
      dad: {
        name:"Sam" , age:49
      }
    }
    }
} >

// The oiutput shold be like this
const Dave:AdamFamily =  {
  area:  "west",
  name: "Dave" , age: 21 , family: {
    sisters:  //!!!!! here i expected that the  key will be part of this obj but it gives me err like it Omit it somehow
     [{ 
      age: 22 , name: neomi
    }],
    parents:{
      mom: {
        name: "Adva" , age: 45
      },
      dad: {
        name:"Sam" , age: 49
      }
    }
    }
}

我只想更改某些值,同时保留对象中的其他值。 当我尝试在该结构中分配时会出现问题,如果我用 ?:

放置一个键则完全忽略
javascript typescript recursion types nested
© www.soinside.com 2019 - 2024. All rights reserved.