Constrain a generic type to be a literal type
<p>My question is two-fold:</p>

<ol>
<li>I would like to know if there is a way in TypeScript to constrain a generic to be a literal type of some kind. What I mean by this is something like <code>function foo&lt;T is a string literal&gt;(...)</code>. The closest I have come to this behaviour is <code>function foo&lt;T extends string&gt;</code>, but this allows unions of string literals and the type "string" itself as values for T.</li>
<li>If this is not possible in TypeScript 2.1, would it make sense from a design perspective to implement such a feature?</li>
</ol>

<p>My use-case is to define a curried function <code>prop</code> like this:</p>

<pre><code>function prop&lt;K extends string, U&gt;(name: K): &lt;T extends { [P in K]: U  }&gt;(obj: T) =&gt; T[K] {
    return (obj) =&gt; obj[name];
}

prop&lt;'name', number&gt;("name")({
    name: 3
})
</code></pre>

<p>This example works as expected if <code>K</code> is a string literal, but the typechecking of the function kind of breaks down <code>K</code> is <code>string</code>.</p>

<p>I know it's a bit contrived; please not my goal is not to solve a practical problem (although it would) but more to experiment with TypeScript's type system.</p>

<p>Thank you!</p>