Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
Replacing Properties in Typescript Type

I am trying to replace properties in a typescript type. Observe the following example:

type WithDifferentKeyValue<
  T,
  K extends keyof T,
  R extends { [Key in K]: any },
> = Omit<T, K> & R;

type Child = {
  wanted: string;
  unwanted: string;
}

type Parent = {
  child: Child;
}

type ModifiedChild = Omit<Child, 'unwanted'>

type ModifiedParent = WithDifferentKeyValue<
  Parent,
  'child',
  { child: ModifiedChild }
>

const invalidModifiedChild: ModifiedChild = {
  wanted: "yay i'm wanted!",
  unwanted: "oh no I'm not wanted!"     // this is correctly marked invalid
}

const invalidModifiedParent1: ModifiedParent = {
  child: invalidModifiedChild           // for some reason this is considered valid
}

const invalidModifiedParent2: ModifiedParent = {
  child: {
    wanted: "yay i'm wanted!",
    unwanted: "oh no I'm not wanted!"   // this is correctly marked invalid
  }
}

I have a Parent which I am trying to replace the child key with the modified child. This modified child should have the key wanted key but omit the unwanted key.

WithDifferentKeyValue is my best attempt at creating a "replacement" helper.

For some reason, my ModifiedParent type will correctly identify errors if the child object is in-line, but it will fail to identify errors if the child object is passed in as a variable. Are there any typescript gurus out there that might be able to explain this to me? Or suggest an alternate solution where ModifiedParent will correctly error on even variables passed in for child? Thanks in advance

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda