Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!