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

100
Views
typescript return destructuring?
function print(): (number, string) {
    return {1,'my'}
}

Above code shows error, I expect I can use const {num, my} = print(). What's the right way to specify return type?

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

0

Try to use a tuple. It looks like this:

function print(): [number, string] {
    return [1,'my']
}

const [num, my] = print();

See demo in Typescript Playground

about 4 years ago · Juan Pablo Isaza Report

0

You need to return an object which has a string and a number. Now, this object should contain property names - so, let's define the return type and use it in your function

type myReturnType = {
  n: number,
  s: string
}

function print():myReturnType {
   return {n:1,s: 'my'}
}

let {n, s} = print();

Here is the shorthand version of the above code:

function print():{n: number, s: string}{
    return {n:1,s: 'my'}
}

let {n, s} = print();
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!