Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda