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

119
Vistas
Type annotations in typescript
interface Test {
   name: string;
   age: number;
}

const subjectObj = {
   name: "Harhsit Singh",
   age: 17,
   height: "6'1",
};

const testObj: Test = subjectObj; //? NO ERROR

const testObj2: Test = {
   name: "Harhsit Singh",
   age: 17,
   height: "6'1", //! ERROR
};

I have created a interface test as shown in the picture, and a test subject named subjectObj and it contains more properties than the type test contains.

Now I have assigned my subjectObj to constant testObj of type test, then declared another constant testObj2 with the same type test and assigned it the same object, but this time initialized it with a new object of same structure as subjectObj.

But now it gives me an error that 'height' does not exist in type 'test', but didn't gave any error when i passed subjectObj as a reference to testObj though it also contains the property height.

I don't know why is this happening, so need some detailed explaination.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Excess property checking happens only when you pass object literals—when you pass in types, you run into the fact that TS never checks for exact types, and assumes that an object with excess types is what you mean.

That's really owing to the JS culture TS is embedded in. It would be really hard to code in TS against JS interfaces otherwise.

about 4 years ago · Juan Pablo Isaza 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