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

147
Views
Generic way of checking if a value of an object is undefined so that the compiler understands it

Given a type T:

type T = {
  akey: string | number | undefined
}

If I check specifically for it

const fun = (p: T) => {
  if(!p.akey) return
  p.akey.toString()
}

Typescript understands that akey can't be undefined.

If I do it generically though:

const fun2 = (p: T) => {
  if(Object.values(p).includes(undefined)) return
  p.akey.toString()
}

It does not: Object is possibly 'undefined'.

How can I make typescript understand so I don't have to do:

p.akey!.toString()

about 4 years ago · Juan Pablo Isaza
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!