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

237
Visualizações
Why doesn't my TypeScript interface alert me that it is missing some properties?

I have a TypeScript interface with required properties.

But when typing an object and pushing it into an array there is no error message alerting me that some of the required properties are missing.

Here is an example from my code:

interface Keyword {
  keyword: string;
  language: string;
  location: string;
  searchEngineDomain: string;
  organicPositionLastThirtyDays: number[];
  createdAt: string;
}

const keywords = [];
keywords.push({
    keyword: 'My First Keyword',
} as Keyword);

I was expecting to see an error telling me the object I am pushing into the array is missing five required properties. But I see no error at all.

Why does this happen?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It's because keywords is of type any[], while it should be Keyword[]. You should also remove as Keyword as it kinda means "believe me, it's all we want here". Type assertions can hide type errors.

Check out the following snippet:

interface Keyword {
  keyword: string;
  language: string;
  location: string;
  searchEngineDomain: string;
  organicPositionLastThirtyDays: number[];
  createdAt: string;
}

const keywords: Keyword[] = [];
keywords.push({
    keyword: 'My First Keyword',
});

You will receive the following error:

Argument of type '{ keyword: string; }' is not assignable to parameter of type 'Keyword'. Type '{ keyword: string; }' is missing the following properties from type 'Keyword': language, location, searchEngineDomain, organicPositionLastThirtyDays, createdAt

about 4 years ago · Santiago Trujillo 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