title: TypeScript - How to define the function interface that accepts both array and object
body: <p>I want to write the function that sorts both array and objects. My attempt is below.</p>
  <pre><code>function sort(obj: any) {
      if (isArray(obj)) {
          // sort as array
      } else if (isObject(obj)) {
          // sort as object
      } else {
          // throw error that primitive value can't be sorted
      }
  }
  </code></pre>
  <p>But it lost the typing at all. I tried to use generic parameters but ran into the errors that <code>obj</code> has no array methods. I tried to use function overloading but ran into the impossibility to distinguish array and objects.</p>