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

382
Visualizações
Typescript: Why destructuring an object doesn't hold type?

In the following, I would expect this to fail as I've stated afternoon should be of type number, but it doesn't fail.

My guess is I'm destructuring incorrectly as hovering over the word afternoon when it returns shows the type as any (in VS Code).

Alternatively, it could all be wrong, this is literally my first play with Typescript, not getting it, yet!

const greetings =  {
    morning: "Good morning.",
    evening: "Good evening.",
    afternoon: "Good afternoon.",
};

interface AboutGreetingTypes {
    morning: string,
    evening: string,
    afternoon: number,
};

const aboutGreeting = ({copy}): AboutGreetingTypes => {
    const d = new Date();
    const timeInHours = d.getHours();

    const { morning, evening, afternoon } = copy;

    if (timeInHours < 12) {
        return morning;
    }
    if (timeInHours > 18) {
        return evening;
    }
    if (timeInHours > 12) {
        return afternoon;
    }
};

console.log(aboutGreeting({copy: greetings}));

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

0

Your function parameter type should be {copy: AboutGreetingTypes} not just AboutGreetingTypes

about 4 years ago · Juan Pablo Isaza Relatório

0

const aboutGreeting = ({copy}): AboutGreetingTypes => {

This line declares a function that accepts a single untyped object and returns an AboutGreetingTypes. Since the argument has no declared type, it is assumed to be any, which lets you do anything without without a type error.


What I think you want is a function that accepts an object that has a copy property that has value of an AboutGreetingTypes.

That would look like this:

const aboutGreeting = ({ copy }: { copy: AboutGreetingTypes }) => {

See playground which still has some errors, but those resolving those are answers to different questions.

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