title: New function constructor that returns another function in TypeScript
body: <p>I have this function in JavaScript:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function test()
  {
    this.list = {};
    return (id) =&gt;
    {
      if (!(id in this.list))
        this.list[id] = {default: 123};
  
      return this.list[id]
    }
  }
  
  const blah = new test();
  blah("ok").mykey = "ok";
  console.log(blah("ok").mykey);
  console.log(blah("ok"));</code></pre>
  </div>
  </div>
  </p>
  <p>And I'm trying convert it into TypeScript format. I understand I'd have to use <code>class</code>, however how do I return a function from the class constructor?</p>
  <p>I don't want have an additional property, <code>myfunc</code>, as a function <code>new test().myfunc(&quot;ok&quot;)</code>:</p>
  <pre class="lang-js prettyprint-override"><code>interface simpleObject
  {
    [key:string]: string|number|object;
  }
  
  class Test
  {
    private list:simpleObject;
  
    public myfunc(id:string)
    {
      if (!(id in this.list))
        this.list[id] = {default: 123};
  
      return this.list[id] as simpleObject;
    }
  
    constructor()
    {
      this.list = {};
    }
  
  }
  
  const blah = new Test();
  blah.myfunc(&quot;test&quot;).mykey = &quot;ok&quot;;
  console.log(blah.myfunc(&quot;test&quot;).mykey);
  console.log(blah.myfunc(&quot;test&quot;))
  </code></pre>
  <p><a href="https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgM7ALYAcA2EDyARgFYQJgBQA3hcsgNoDWEAngFyphSgDmAumzRdeAHxABXDIWgiA9iTJgA3BQC+FBDjipUySJ2q1kWbgDc4kZDmCcOmXAQXkVdLOMLWEyDCxjiQCAAUwAAmHMIgPACURjR0dMAwyIEAhMEhyKB6ABY2AHTWnFEx8fFguagFNmD0oXzIALzIVCEQ8OI4YIIAjABMAMyqKkZ0UBBg4lAgOfmFNXXI2mj2eESkzkbqdAiyIJxQ4uSyUIElzSMzlXONzUObahQau5zIHnDZNyAQAO56EJynFRvbJ5Hx+AKBABE+jAkKioJYzBYN0hskYkJUOz2sjwBVkPECwIR4KC0P+sPhPiRUUxzxxEDxBKJYP8pJhcKiQA" rel="nofollow noreferrer">On TS Playground</a></p>