title: Using TypeScript with body-parser to read req.body properties
body: <p>I'm building a web app using TypeScript with the body-parser middleware to parse JSON request bodies. I'm running into type errors when I try to access properties on the <code>Request.body</code> object.</p>
  
  <p>For example, if I try, <code>req.body.username</code>, TypeScript throws an error</p>
  
  <pre class="lang-js prettyprint-override"><code>Property 'username' does not exist on type 'ReadableStream&lt;Uint8Array&gt;'. 
  </code></pre>
  
  <p>Things I tried:</p>
  
  <ul>
  <li>Declaring a custom interface for <code>ReadableStream&lt;Uint8Array&gt;</code>, just            before accessing the <code>username</code> property.</li>
  </ul>
  
  <pre class="lang-js prettyprint-override"><code>interface ReadableStream&lt;Uint8Array&gt; {
      username: string
  }
  </code></pre>
  
  <p>TypeScript informs me that <code>ReadableStream</code> was declared but never used. Cannot figure out why this is the case. </p>
  
  <ul>
  <li>Wrapping usage in an <code>if</code> block</li>
  </ul>
  
  <pre class="lang-js prettyprint-override"><code>if (req.body.hasOwnProperty('username')) {
      // Use req.body.username here
  }
  </code></pre>
  
  <p>TypeScript does not seem to understand that this implies that the property username exists.</p>
  
  <ul>
  <li>Using indexing instead of dot notation</li>
  </ul>
  
  <pre class="lang-js prettyprint-override"><code>let p_username = req.body['username'];
  </code></pre>
  
  <p>This was the last thing I tried, even though it defeats the point of typechecking. Even this does not work:</p>
  
  <pre class="lang-js prettyprint-override"><code>Element implicitly has an 'any' type because type 'ReadableStream&lt;Uint8Array&gt;' has no index signature.
  </code></pre>
  
  <p>It seems like reading the request body contents like this is fairly standard usage, and so there should be a simple way to get past this problem. Maybe I'm missing something obvious. </p>