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

211
Visualizações
TS2339: Property 'email' does not exist on type 'FindUserProps'
interface FindUserEmailProps {
   readonly email: string
}

interface FindUserIdProps {
   readonly id: string
}

type FindUserProps = FindUserEmailProps | FindUserIdProps

export const findUserByEmail = async ({ email }: FindUserProps): Promise<IUser> => {
   const user = await User.findOne({ email })
   if (!user) {
      throw new Error('User not found')
   }
   return user
}

At email property I got TS2339: Property 'email' does not exist on type 'FindUserProps' Why is that?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

That's because FindUserProps can be one of FindUserEmailProps or FindUserIdProps but it's not both (that would be FindUserEmailProps & FindUserIdProps). That means that TypeScript doesn't know which one it is until you assert it.

Your function has to take a FindUserProps and needs to add your own type guard to let TypeScript know whether it's a FindUserEmailProps or FindUserIdProps before you can extract an email property.

// Custom type guard that lets TypeScript know whether your
// object is a FindUserEmailProps
function isFindUserEmailProps(obj: FindUserProps): obj is FindUserEmailProps {
  return "email" in obj;
}

export const findUserByEmail = async (userProps: FindUserProps): Promise<IUser> => {
   // You have to make sure it's a FindUserEmailProps
   if (!isFindUserEmailProps(userProps)) {
      // Since you are just throwing an error, you may as well
      // change your function to only accept a FindUserEmailProps
      throw new Error("Invalid userProps to find by email");
   }
   const {email} = userProps;
   const user = await User.findOne({ email })
   if (!user) {
      throw new Error('User not found')
   }
   return user
}
over 4 years ago · Santiago Trujillo Relatório
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