title: Typescript check object by type or interface at runtime with typeguards in 2020+
body: <p>Most of the time for me, dynamic check is needed for verification of fetch response. And i was thinking, can this be done with user defined typeguard in a generic way for any type of object with multiple props and additional checks, so it can be used something like:</p>
  
  <p><a href="https://www.typescriptlang.org/play/index.html?ssl=1&amp;ssc=1&amp;pln=41&amp;pc=43#code/PTAEF5K6c0GcAWBDATgUwCanQDwA4D2qALlhLJVAFAkCe%206oA6qgJZmoSgDe1ooAHbIAtugBcCEu0EBzfqGSyJQgK4iARulTUAvtVoMmAIUKEA1tz4C2mSYPVadA4WMnxpbOQpJL47zzkAbQBdUAAfNQAbKIUAd3ZOfxZE7VC9AxBQADNVQQBjEjZCQVASQlB8xHR8y0INACsaklA4jkRQEVUoovwopnxUQnx4UGVBbWQohBRGABoyisFCFswKjlANOlAUQUxqXIKiktBCbIAVIwAec4A%20AAp6hslkQToASkkn0DZR894FGxsqB7gBCJ7vUAYEiqVClbJTeDoADcCgUoCyiEIcUWAH50dDYaVpKomFliDlEUwMtQslR6aBVEjsK9sJh0NkvOQvJVCCIiBNBCRRvSqAc8oViqVfqYLI9Gi83p9To0fqNZZZrD9gWCzpdGFcNQ8IZDCXDKVEkRjgFUauYvLIchSkMhGIo9gg2Pz%20mUjPADAIsrb7XInVx4Ix8kC2PlQAA3Kak0asqGvZT%20mw6p4AOlsoCuoAAjKb0DDzQjLdTMyCcwkONpRgAyRsqhrZutJbPwPnoe73XiKZSgXSgd7gW6DpgFgCsEUiSiYE8L04ADO8S2X4VTQAGoaWiWVUKSaQJqPkSh5NmZLOAATY7EW5gpXCoAOQAQQTcgbr6fAl8sjJEEr4iGgcboFEv6gK%20hyviEf6tKkqBAbwL6SK%20ACyyC4FBC6SAATNOugLDwaHQVhOELHhoCEboIQ0uegjdv02ZRIQsj3DK173Bo17rtah6kqcXAVkiQA" rel="noreferrer">Typescript playground</a>.</p>
  
  <p><a href="https://stackoverflow.com/questions/48442746/check-if-object-correctly-implements-interface">Here is an example with sample object, but i want a function without it.</a></p>
  
  <pre><code>// ================= shared exported =================
  type Writer = {
    name: string
    age: number
  }
  
  type Book = {
    id: number
    name: string
    tags: string[] | null
    writers: Writer[]
  }
  
  // function to check object with multiple props general shape, to not do it by hand
  function ofType&lt;T&gt;(obj: any): obj is T {
    if (!obj) return false;
  
     // how to?
     return true // or false 
  }
  
  // ================= used and defined in components =================
  function isBook(obj: any): obj is Book {
    if (!ofType&lt;Book&gt;(obj)) return false //checking for shape and simple types
  
    // cheking for specific values and ranges
    if (obj.id &lt; 1) return false 
    if (obj.writers &amp;&amp; obj.writers.some(( { age } )=&gt; age &lt; 5 || age &gt; 150)) return false 
  
    return true
  }
  
  
  const book = {
    id: 1,
    name: 'Avangers',
    tags: ['marvel', 'fun'],
    writers: [ {name: 'Max', age: 25}, {name: 'Max', age: 25}]
  }
  
  console.log(isBook(book)) // true or false
  </code></pre>