title: TypeScript definition for pluck
body: <p>The <a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#index-types" rel="noreferrer">TypeScript documentation</a> gives the following example for defining a <code>pluck</code> function, using "index types" (slightly modified):</p>
  
  <pre><code>function pluck&lt;T, K extends keyof T&gt;(o: T, name: K): T[K] {
    return o[name];
  }
  </code></pre>
  
  <p>which is obviously called as</p>
  
  <pre><code>pluck({a: 1}, 'a')
  </code></pre>
  
  <p>However, I want to define a <code>pluck</code> which takes just a property name, and yields a function I can apply to an object to obtain that property, as in</p>
  
  <pre><code>const getA = pluck('a');
  getA({a: 1})
  </code></pre>
  
  <p>I tried the following:</p>
  
  <pre><code>function pluck&lt;T, K extends keyof T&gt;(p: K) {
    return function(o: T): T[K] {
      return o[p];
    };
  }
  </code></pre>
  
  <p>This compiles fine, but when I try to use it:</p>
  
  <pre><code>const a = {a: 1};
  const getA = pluck('a');
  console.log(getA(a));
  </code></pre>
  
  <p>I get the error</p>
  
  <blockquote>
    <p>Argument of type '"a"' is not assignable to parameter of type 'never'. [2345]</p>
  </blockquote>
  
  <p>on the line where I call <code>pluck('a')</code>. Is there any way to do what I want?</p>