<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: George C. Norris</title>
    <description>The latest articles on DEV Community by George C. Norris (@georgenorris).</description>
    <link>https://dev.to/georgenorris</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F103063%2F5d4b869a-bde0-4179-bda9-d280172e355a.jpeg</url>
      <title>DEV Community: George C. Norris</title>
      <link>https://dev.to/georgenorris</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/georgenorris"/>
    <language>en</language>
    <item>
      <title>How to extend and use an extended Request type in Typescript / Express?</title>
      <dc:creator>George C. Norris</dc:creator>
      <pubDate>Thu, 17 Sep 2020 20:47:51 +0000</pubDate>
      <link>https://dev.to/georgenorris/how-to-extend-and-use-an-extended-request-type-in-typescript-express-3o8f</link>
      <guid>https://dev.to/georgenorris/how-to-extend-and-use-an-extended-request-type-in-typescript-express-3o8f</guid>
      <description>&lt;p&gt;How to extend and use an extended Request type in Typescript / Express?&lt;/p&gt;

&lt;p&gt;I'm adding a BUNCH of middleware libraries that extend the Request Object. I.E. One added user to req. Another add cookie() to req. Another add csrfToken() to req. etc..&lt;/p&gt;

&lt;p&gt;When I add a request handler function, how do I tell that function to use the req with all the bells and whistles added by middleware?&lt;/p&gt;

&lt;p&gt;Do I hunt down every DefinitelyTyped package corresponding to the middleware? If so, then will the Request type be magically 'decorated` with these properties?&lt;/p&gt;

&lt;p&gt;To make it even harder, I've written my own middleware that adds properties to Request&lt;/p&gt;

&lt;p&gt;&lt;code&gt;req.myCustomFunction()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In this case, will I need to declare, and extend the Request myself with myCustomFunction?&lt;/p&gt;

&lt;p&gt;In addition, will that Request which I am extending 'include' the types given by DefinitelyTyped?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
declare namespace Express {&lt;br&gt;
  export interface Request {&lt;br&gt;
    myCustomFunction: () =&amp;gt; void&lt;br&gt;
  }&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Will this now include ALL the properties included via DefinitelyTyped AND my myCustomFunction?&lt;/p&gt;

&lt;p&gt;How do I reference this interface when using?&lt;/p&gt;

&lt;p&gt;Will it be Express.Request? Or just Request?&lt;/p&gt;

&lt;p&gt;If I reference it as Request, how does Typescript know to use "my" Request and not the one exported by Express's DefinitelyTyped library?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
declare namespace Express {&lt;br&gt;
  export interface Request {&lt;br&gt;
    import_destination?: string;&lt;br&gt;
    isZipImport?: boolean;&lt;br&gt;
    method: string;&lt;br&gt;
    originalname: string;&lt;br&gt;
    unzippedPath?: string;&lt;br&gt;
    url: string;&lt;br&gt;
    user?: TUser;&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;DOES NOT WORK&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
declare module global {&lt;br&gt;
  interface Request {&lt;br&gt;
    file?: FileObj;&lt;br&gt;
    files?: FileObj[];&lt;br&gt;
    user?: TUser;&lt;br&gt;
    isZipImport?: boolean;&lt;br&gt;
    unzippedPath?: string;&lt;br&gt;
  }&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
DOES NOT WORK&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
declare global {&lt;br&gt;
  declare module 'express' {&lt;br&gt;
    export interface Request {&lt;br&gt;
      file?: FileObj;&lt;br&gt;
      files?: FileObj[];&lt;br&gt;
      user?: TUser;&lt;br&gt;
      isZipImport?: boolean;&lt;br&gt;
      unzippedPath?: string;&lt;br&gt;
    }&lt;br&gt;
  }&lt;br&gt;
}&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;DOES NOT WORK&lt;/p&gt;

&lt;p&gt;`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
declare namespace Express {&lt;br&gt;
  export interface Request {&lt;br&gt;
    user?: TUser;&lt;br&gt;
    isZipImport?: boolean;&lt;br&gt;
    unzippedPath?: string;&lt;br&gt;
  }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;`&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


DOES NOT WORK


I'm seriously pulling my hair out trying to get this work. 
WHY WHY WHY.. is this so complicated to make work????
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

</description>
      <category>help</category>
    </item>
  </channel>
</rss>
