2

For example, I want to check if an object has the following format

interface InterA = { a: {
    b: {
      c: string //Always has this
      // May have other properties here as well, like d: number, e: string
    }
  d: string //Always has this as well
}

I would want it to recognize that this object is of that type.

{ a: {
    b: {
      c: 'Always exists'
      d: 'Exists this time
      e: 42
    }
  d: 'Also always exists'
}

I want to check if an object is an instanceof that object, and return the object and all its parameters, not just the ones defined in the interface. I was thinking the best way to do this was to use a type guard or have the function tests for a but couldn't get either of them to work. What would be the best way to do this?

  • You can't check if arbitrary objects match an interface, because types do not exist at runtime. So this is a manual process. – Evert Aug 13 '20 at 18:18
  • 1
    As much as I'd like to take credit for a cool solution to this, this question has already been answered here: https://stackoverflow.com/questions/14425568/interface-type-check-with-typescript – The James Aug 13 '20 at 18:28

0 Answers0