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

298
Views
Can I pick type of return data by argument in TypeScript?

can we predict types by function argument passed to it ?

Let's imagine we have fn that will accept one argument (number) and depends on that number I will return collection or single entity from database. Result will differ in model slightly and I would tell to TypeScript that if such number passed to fn argument exists, return me model A otherwise return me model B.

Fn example:

const fn = (id?: number) => {
  // body of fn
}

const myCollectionData = fn(); // interface A
const mySingleRecordData = fn(1); // interface B

Cheers!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Sure, you can declare overloads for that function:

const fn: {
  (): interfaceA;
  (id: number): interfaceB;
} = (id?: number) => {
  // body of fn
};

(see Typescript overload arrow functions for this particular syntax)

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