title: How to access object property with dynamic key in Typescript?
body: <p>In my component <code>Product.tsx</code> I receive an object via API call that could look like this:</p>
  <pre><code>{
    title: &quot;Product 1&quot;,
    status: &quot;active&quot;,
    tax: true,
    variants: [
      {
        id: 1,
        sku: 'PRD1',
        price: 24.00,
        price_reduced: 19.95,
        size: 'M'
      },
      {
        id: 2,
        sku: 'PRD2',
        price: 24.00,
        price_reduced: 19.95
        size: 'L'
      },
    ]
  }
  </code></pre>
  <p>I then render each <code>variant</code> as a row in a table where each column displays the price, size etc. as editable input fields. <code>onChange</code> of each <code>input</code> triggers <code>updateVariant(variant.id, key, e);</code> where <code>key</code> is one of the keys of a <code>variant</code> and <code>e</code> is the input event.</p>
  <p><code>updateVariant</code> should update the value of a <code>variant</code> with the given <code>key</code> and looks like this:</p>
  <pre><code>  const updateVariant = (
      id: number,
      key: string,
      e: React.FormEvent&lt;HTMLInputElement&gt;
    ) =&gt; {
      setProduct((prevState) =&gt; {
        const update = { ...prevState };
        const i = update.variants.findIndex((variant) =&gt; variant.id === id);
        const updatedVariant = update.variants[i];
        updatedVariant[key] = e.currentTarget.value;   // errors on this line
        update.variants[i] = udpatedVariant;
        return update;
      });
    };
  </code></pre>
  <p>It gives me  an error on <code>updatedVariant[key] = e.currentTarget.value;</code>:</p>
  <pre><code>Element implicitly has 'any' type because expression of type 'string | number' can't be
  used to index type '{id: number, sku: string, ...}'. No index signature with a parameter
  of type 'string' was found on type '{id: number, sku: string ...}'
  </code></pre>
  <p>I tried changing it to:
  <code>updatedVariant[key as keyof typeof updatedVariant] = e.currentTarget.value;</code>
  which gives me the error <code>type 'any' is not assignable to type 'never'</code></p>
  <p>I am quite new to Typescript and completely lost how to make this work.</p>
  <p><strong>EDIT:</strong>
  I got it to work temporarily with</p>
  <pre><code>updatedVariant[key as keyof typeof updatedVariant] = e.currentTarget.value as never;
  </code></pre>
  <p>But this can't be the right / clean solution to this?</p>