Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda