Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
TS2339: la propiedad 'correo electrónico' no existe en el tipo '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 }

En la propiedad de correo electrónico obtuve TS2339: la propiedad 'correo electrónico' no existe en el tipo 'FindUserProps' ¿Por qué?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Esto se debe a que FindUserProps puede ser uno de FindUserEmailProps o FindUserIdProps , pero no ambos (eso sería FindUserEmailProps y FindUserIdProps ). Eso significa que TypeScript no sabe cuál es hasta que lo afirma.

Su función debe tomar FindUserProps y debe agregar su propia protección de tipo para que TypeScript sepa si se trata de FindUserEmailProps o FindUserIdProps antes de que pueda extraer una propiedad de email .

 // 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!