title: Adding to existing library typescript types with a new definition file
body: <p>I’m using this library <a href="https://github.com/chentsulin/koa-bearer-token" rel="noreferrer">https://github.com/chentsulin/koa-bearer-token</a> which adds an extra property to the koa libraries request object like <code>ctx.request.token</code>. So if I use the koa types directly I get an error which tells me the <code>token</code> property doesn’t exist on <code>ctx.request.token</code>.</p>
  
  <h3>My current solution</h3>
  
  <p>I created a type definition file called <code>koa-bearer-token.d.ts</code> which contains types for the library and exports for the extended koa context/request type:</p>
  
  <pre><code>declare module 'koa-bearer-token' {
      import {Context, Request, Middleware} from 'koa';
  
      interface Options {
          queryKey?: string;
          bodyKey?: string;
          headerKey?: string;
          reqKey?: string;
      }
  
      interface RequestWithToken extends Request {
          token?: string
      }
  
      interface ContextWithToken extends Context {
          request: RequestWithToken
      }
  
      export default function bearerToken(options?: Options): Middleware;
      export {RequestWithToken, ContextWithToken};
  }
  </code></pre>
  
  <p>Then I use this in other files like:</p>
  
  <pre><code>import {ContextWithToken} from 'koa-bearer-token';
  const someFunction = (ctx: ContextWithToken) =&gt; {
      const token = ctx.request.token; // &lt;-- No longer errors
  };
  </code></pre>
  
  <h3>Why I'm asking this question</h3>
  
  <p>This works now but I’m concerned it isn’t the best way because it wouldn’t work if I need to add more properties in the future, ideally I want to just create a <code>koa.d.ts</code> file that adds to the libraries types then I can carry on using <code>import {Context} from 'koa';</code> instead of <code>import {ContextWithToken} from 'koa-bearer-token';</code> but when I create <code>koa.d.ts</code> it overwrites all the library types instead of adding on top of them.</p>
  
  <p>Here is my tsconfig.json in case it helps</p>
  
  <pre><code>{
    "compilerOptions": {
      "module": "commonjs",
      "esModuleInterop": true,
      "target": "es6",
      "noImplicitAny": true,
      "moduleResolution": "node",
      "sourceMap": true,
      "outDir": "dist",
      "baseUrl": ".",
      "paths": {
        "*": [
          "node_modules/*",
          "src/@types/*"
        ]
      }
    },
    "include": [
      "src/**/*"
    ]
  }
  </code></pre>