title: Typescript generic class in object
body: <p>I want to do a generic typescript class. But it doesn't work when the class is inside an object. I've tried a lot of codes, but they don't work for me :(</p>
  <p>I have tried all of this.</p>
  <pre class="lang-js prettyprint-override"><code>// this is my class object
  const obj1 = {
      Foo: class&lt;T&gt; {
          foo: T
  
          constructor({ foo }: { foo: T }) {
              this.foo = foo;
          }
  
          getFoo(): T {
              return this.foo
          }
  
          setFoo(t: T): T {
              this.foo = t
              return this.foo
          }
      }
  }
  
  
  // This works: it is typechecked
  let a = new obj1.Foo({ foo: 0 })
  a.getFoo()      // number
  a.setFoo('123') // error
  
  
  // None of this works
  // uncomment them to see errors
  
  // let b: obj1.Foo                                      = new obj1.Foo({ foo: 0 })
  // let b: obj1.Foo&lt;number&gt;                              = new obj1.Foo({ foo: 0 })
  // let b: typeof obj1.Foo                               = new obj1.Foo({ foo: 0 })
  // let b: typeof obj1.Foo&lt;number&gt;                       = new obj1.Foo({ foo: 0 })
  // let b: typeof obj1.Foo.prototype                     = new obj1.Foo({ foo: 0 })
  // let b: typeof obj1.Foo.prototype&lt;number&gt;             = new obj1.Foo({ foo: 0 })
  // let b: typeof obj1[&quot;Foo&quot;][&quot;prototype&quot;]               = new obj1.Foo({ foo: 0 })
  // let b: typeof obj1[&quot;Foo&quot;][&quot;prototype&quot;]&lt;number&gt;       = new obj1.Foo({ foo: 0 })
  
  
  b.getFoo()      // any
  b.setFoo('123') // works (it should error instead)
  </code></pre>
  <p><a href="https://www.typescriptlang.org/play/?declaration=false&amp;target=99&amp;ssl=1&amp;ssc=1&amp;pln=41&amp;pc=1#code/MYewdgzgLgBCBGArAjDAvDA3gKBnmAYiCAFwzAA2AhhBADwAqAfFrvuwGbFkPZvt5QkKACcArsCggRACkwwuIGAF8y8xTxUBKVgL0woACwCWEAHSL0C4gG5+A5X30wA5gFMoREDK2acz9hEPMREwAxNzRXt2R2j8CA8vGSgeXxgGXQC8I1MLYisoOL0gqBCwnMjiIpV7R1iAenr0iJgAd2kAawgyY1hTAwBPAAc3YENRjrcAE2wKDxgqKzA3VrgkZDMk9W4YAAZtbCozd09iHz1GmDAxAFt4NxFDswTT7wByZAAmAGY3nUuHiJpHxsJcAHLgNxwDjhKFcCgUECtYxgFwLIKDEZjCbTUFNMRgUA3G5uMCwIxuG4GJQJKGA6QQPiXOaweBkBAoTb5LL6ACQPIEGGWqw5Gy21lIewOzPmbLWnK8dGudweLHY-IFPKFK3lYrO20l+2UWjxMBZMDlUGGbhAMNFXKUmqd+G1IvWDrkErIRpNMtZZCtI1tuodStu9xEaudeldIfFGilxtN5st1uD9q8ZiGQKkgah0fYsYz+q9id9TRTAbTdvdmezIFz1rDKsjmqLtZLCZ9ydlVaDNZQAG0AEReYcAXRH9cbIwnzvbCs7O27fotfZtA+QI7Hk+H04b1onzYjUcLVx1xe8Bu9Bz48GOiTOOgElyoYAG2HvLySHx+fxglztCIXQwDIvQwBAhggGIFBTDA9IiDAKLQG4VBTCaQA" rel="nofollow noreferrer">typescript playground link</a></p>
  <p>thanks but sorry for bad english</p>