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

124
Views
TypeScript: how to make a property conditional from an existing type?

So let's say I have

type Link = {
  text: string;
  link: string;
}

interface BigLink extends Link {
  some: number;
  something: string;
  else: string;
}

but I have a variable that shares all these properties except that the link property can be optional. I don't want to create a whole new type just to change one field to link?: string. Is there a way to do something like

type OptionalBigLink = BigLink & { link?: string }

so that I can overwrite the link field and make it optional. ^ The above code still throws an error when I don't pass in the link property.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Yes you can.

type OptionalBigLink = Partial<Pick<BigLink, "link">> & Omit<BigLink, "link">;

Some tests in the playground

about 4 years ago · Santiago Trujillo Report

0

You can use Omit to remove the property from BigLink and then do the intersection.

type OptionalBigLink = Omit<BigLink, "link"> & { link?: string }
about 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!