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

236
Visualizações
Typescript: How to be typesafe on exempting an enum value in new interface

I'm currently working on creating a new method that wants to do an exemption of a specific enum value.

import { Overwrite } from 'utility-types';

enum Gender {
  boy = 'boy',
  girl = 'girl',
}

type Human = {
  gender: Gender,
  age: number,
}

type BoyGender = Exclude<Gender, 'girl'>;

type HumanBoy = Overwrite<
  Human,
  {
    gender: BoyGender;
  }
>;

export function accept(human: HumanBoy): void {
  console.log(human)
}

accept({
  gender: Gender.boy, // type error here
  age: 2
})

When I use the enum of Gender again, I get a type error saying that

Types of property 'gender' are incompatible. Type 'Gender' is not assignable to type 'BoyGender'

Currently not sure why this is happening since BoyGender gets its value from Gender enum with the exemption of girl. Is there a way to pass this type error rather than creating a new enum with only boy?

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