title: Is it possible to constrain a generic type to be a subset of keyof in TypeScript?
body: <p>In the current version (2.1) of TypeScript I can constrain a method argument on a generic class to be a property of the generic type.</p>
  
  <pre><code>class Foo&lt;TEntity extends {[key:string]:any}&gt; {
      public bar&lt;K extends keyof TEntity&gt;(key:K, value:TEntity[K]) { }
  }
  </code></pre>
  
  <p>Is it possible in the current type system to constrain the key part even further to be a subset where the value of the key is of a certain type?</p>
  
  <p>What I'm looking for is something along the lines of this psuedo code.</p>
  
  <pre><code>class Foo&lt;TEntity extends {[key:string]:any}&gt; {
      public updateText&lt;K extends keyof TEntity where TEntity[K] extends string&gt;(key:K, value:any) {
          this.model[key] = this.convertToText(value);
      }
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>For clarification I added a more complete example of what I'm trying to achieve.</p>
  
  <pre><code>type object = { [key: string]: any };
  
  class Form&lt;T extends object&gt; {
      private values: Partial&lt;T&gt; = {} as T;
  
      protected convert&lt;K extends keyof T&gt;(key: K, input: any, converter: (value: any) =&gt; T[K])
      {
          this.values[key] = converter(input);
      }
  
      protected convertText&lt;K extends keyof T&gt;(key: K, input: any)
      {
          this.values[key] = this.convert(key, input, this.stringConverter);
      }
  
      private stringConverter(value: any): string
      {
          return String(value);
      }
  }
  </code></pre>
  
  <p><a href="https://www.typescriptlang.org/play/index.html#src=type%20object%20%3D%20%7B%20%5Bkey%3A%20string%5D%3A%20any%20%7D%3B%0D%0A%0D%0Aclass%20Form%3CT%20extends%20object%3E%20%7B%0D%0A%20%20%20%20private%20values%3A%20Partial%3CT%3E%20%3D%20%7B%7D%20as%20T%3B%0D%0A%0D%0A%20%20%20%20protected%20convert%3CK%20extends%20keyof%20T%3E(key%3A%20K%2C%20input%3A%20any%2C%20converter%3A%20(value%3A%20any)%20%3D%3E%20T%5BK%5D)%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20this.values%5Bkey%5D%20%3D%20converter(input)%3B%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20protected%20convertText%3CK%20extends%20keyof%20T%3E(key%3A%20K%2C%20input%3A%20any)%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20this.values%5Bkey%5D%20%3D%20this.convert(key%2C%20input%2C%20this.stringConverter)%3B%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20private%20stringConverter(value%3A%20any)%3A%20string%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20return%20String(value)%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A" rel="noreferrer">Demo on typescriptlang.org</a></p>
  
  <p><code>convertText</code> will give an error saying that <code>Type 'string' is not assignable to type 'T[K]'</code>.</p>
  
  <p>Given</p>
  
  <pre><code>interface Foo {
      s: string
      n: number
  }
  </code></pre>
  
  <p>The compiler can tell that this will work</p>
  
  <pre><code>this.convert('s', 123, v =&gt; String(v));
  </code></pre>
  
  <p>and this will not</p>
  
  <pre><code>this.convert('n', 123, v =&gt; String(v));
  </code></pre>
  
  <p>I'm hoping I can constrain the <code>convertText</code> method to keys where the value is of type <code>string</code> to get type safety on the key parameter.</p>