Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda