title: How to tell typescript to accept an empty array or a function
body: <p>Consider the following code:</p>
  <pre class="lang-js prettyprint-override"><code>const subscriptions: { [key: string]: [() =&gt; void]  } = {}
  
  interface ISubscription {
    eventName: string
    callback: () =&gt; void
  }
  
  export function unsubscribe({ eventName, callback }: ISubscription) {
    if (!subscriptions[eventName]) { return }
    const index = subscriptions[eventName].findIndex((l) =&gt; l === callback)
    if (index &lt; 0) { return }
    subscriptions[eventName].splice(index, 1)
  }
  
  export function subscribe({ eventName, callback }: ISubscription) {
    if (!subscriptions[eventName]) {
      subscriptions[eventName] = []
    }
    subscriptions[eventName].push(callback)
    return () =&gt; unsubscribe({ eventName, callback })
  }
  </code></pre>
  <p>The issue typescript reports is:</p>
  <blockquote>
  <p>TS2741: Property '0' is missing in type '[]' but required in type '[() =&gt; void]'.</p>
  </blockquote>
  <p>Which comes from trying to assign an empty array to the <code>subscriptions</code>:</p>
  <pre class="lang-js prettyprint-override"><code>if (!subscriptions[eventName]) {
    subscriptions[eventName] = []
  }
  </code></pre>
  <p>This can be fixed by defining the interface to accept an empty array but then there's an issue when assigning a callback:</p>
  <pre class="lang-js prettyprint-override"><code>const subscriptions: { [key: string]: [() =&gt; void] | [] } = {}
  </code></pre>
  <blockquote>
  <p>TS2345: Argument of type '() =&gt; void' is not assignable to parameter of type 'never'.</p>
  </blockquote>
  <p>A workaround would be:</p>
  <pre class="lang-js prettyprint-override"><code>const subscriptions: { [key: string]: [() =&gt; void] } = {}
  
  if (!subscriptions[eventName]) {
    subscriptions[eventName] = [callback]
  }
  else {
    subscriptions[eventName].push(callback)
  }
  </code></pre>
  <p>What is the correct way to be able to set it to an empty array and then assign the callback? I tried using the question mark too but I can't seem to get it right. Thank you for your help.</p>