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

597
Visualizações
How to assign a variable of type (x:number)=>{y:number,z:number}?

I have a variable foo to be initialized. My attempt below does not compile.

let foo: (x: number)=>{y:number,z: number}= (x)=>{x+1,x+2};

with the following error:

Left side of comma operator is unused and has no side effects.ts(2695) (parameter) x: number

Question

What is the correct way to initialize the variable foo?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Because of how arrow function notation works, the curly braces around the output are interpreted as containing the function body instead of a value being returned.

There are two ways around this. One is to use the longer arrow function notation, with a return statement. The other is to wrap your object in brackets.

It also looks like you've forgot to add the y and z labels in your output.

Try one of these:

let foo: (x: number) => {y: number, z: number} = (x) => ({y: x+1, z: x+2});
let foo: (x: number) => {y: number, z: number} = (x) => {
    return {y: x+1, z: x+2};
};

It's also worth noting that when initialising a variable in this way, TypeScript is able to infer its type. So you can actually shorten your code to just one of these:

let foo = (x: number) => ({y: x+1, z: x+2});
let foo = (x: number) => {
    return {y: x+1, z: x+2};
};
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