title: TypeScript: reduce function - No overload matches this call
body: <p>Trying to write a basic reducer to return an array of (value of) a key from an array of objects. Some of the key can be missing or undefined.</p>
  <p>My code:</p>
  <pre><code>const data = [{Key: 56}, {Key: undefined}, {}, {Key: 44}]
  
  const keys = data.reduce((prev, curr) =&gt; {
    if (curr.Key) {
      return [...prev, curr.Key];
    } else return prev;
  }, []);
  </code></pre>
  <p>It works fine as a plain JS but the TS compiler does not like it. <a href="https://www.typescriptlang.org/play?#code/MYewdgzgLgBAJgQygmBeGBtA3gaQKYCeAXDAKwBsAvgDQy6EkCuYceAZgJZh5w11-1iMACzDKAXQBQk0JFgBrQhDTwkCAHQAnHo2B4AFPoAO2gG61gjTZoCUaAHx1JMGBzYx9l6+vwE7WZxcYbSgrMEx1SJM8cxgvTR9CcQBuQMoYPAAbCDxgvFDNcOjTVL4McRtkoA" rel="nofollow noreferrer">Playground Link</a></p>
  <p>How to fix this? mainly what am I doing wrong? I am a TS noob.</p>
  <p>Error:</p>
  <pre><code>No overload matches this call.  
  
  Overload 1 of 3, '(callbackfn: (previousValue: { Key: number; } | { Key: undefined; } | { Key?: undefined; }, currentValue: { Key: number; } | { Key: undefined; } | { Key?: undefined; }, currentIndex: number, array: ({ Key: number; } | { ...; } | { ...; })[]) =&gt; { ...; } | ... 1 more ... | { ...; }, initialValue: { ...; } | ... 1 more ... | { ...; }): { ...; } | ... 1 more ... | { ...; }', gave the following error.  
  Argument of type '(prev: never[], curr: { Key: number; } | { Key: undefined; } | { Key?: undefined; }) =&gt; number[]' is not assignable to parameter of type '(previousValue: { Key: number; } | { Key: undefined; } | { Key?: undefined; }, currentValue: { Key: number; } | { Key: undefined; } | { Key?: undefined; }, currentIndex: number, array: ({ Key: number; } | { ...; } | { ...; })[]) =&gt; { ...; } | ... 1 more ... | { ...; }'.  
  Types of parameters 'prev' and 'previousValue' are incompatible.  
  Type '{ Key: number; } | { Key: undefined; } | { Key?: undefined; }' is not assignable to type 'never[]'.  
  Type '{ Key: number; }' is missing the following properties from type 'never[]': length, pop, push, concat, and 26 more.  
  Overload 2 of 3, '(callbackfn: (previousValue: never[], currentValue: { Key: number; } | { Key: undefined; } | { Key?: undefined; }, currentIndex: number, array: ({ Key: number; } | { Key: undefined; } | { Key?: undefined; })[]) =&gt; never[], initialValue: never[]): never[]', gave the following error.  
  Argument of type '(prev: never[], curr: { Key: number; } | { Key: undefined; } | { Key?: undefined; }) =&gt; number[]' is not assignable to parameter of type '(previousValue: never[], currentValue: { Key: number; } | { Key: undefined; } | { Key?: undefined; }, currentIndex: number, array: ({ Key: number; } | { Key: undefined; } | { Key?: undefined; })[]) =&gt; never[]'.  
  Type 'number[]' is not assignable to type 'never[]'.  
  Type 'number' is not assignable to type 'never'.
  </code></pre>