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

344
Vistas
How do you ensure an object conforms to a Typescript interface?

how I could validate that an object has all the properties of an interface.

For example:

interface Vehicle {
 name: string 
 model: string
}

interface User {
 Name: string
 age: númber
}

interface Customer extends User  {
 vehicles: Vehicle[]
}

How could I check that an object has all the properties of Customer interface????

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

0

if you mean static type checking...

If by "validate that an object has all the properties of an interface" you mean static type checking, well that's Typescripts job and it will do that at compile time if you use try to assign the object to a variable of that interface type. e.g.:

const obj = {name: 'Edwin', age: 35}

let c: Customer = obj // this will fail at compile time

if you mean runtime type checking...

Typescript at runtime is just Javascript, and Javascript does not have a first class support for interfaces. In fact, Vehicle, User and Customer will not even exist at runtime because of type erasure. For more info see these this and this in the Typescript Handbook and docs. So you cannot reference these types in any runtime logic. The Javascript instanceof operator only works for Javascript prototype inheritance, and has very specific meaning:

The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value.

~ instanceof | MDN

The Javascript typeof operator only works for primitive types, like string.

If you need to do runtime "type checking", you will have to do that manually, checking for the existence of each field, and its type recursively.

Typescript has some syntactic sugar to make such manual checks look natural in your Typescript source code. Read about Narrowing and Type Guards.

There my even be a library that implements that for you. In fact I do remember coming across a library that generates runtime type checking based on Typescript definition... Let me find it really quick...

Some examples (there are more):

  • https://github.com/gcanti/io-ts
  • https://github.com/pelotom/runtypes
  • https://github.com/fabiandev/ts-runtime
  • https://github.com/janjakubnanista/ts-type-checked
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