title: How do I write a Typescript definition file for a JavaScript library written like the following?
body: <p>I have an old JavaScript library that I wrote to work with <code>AMD</code> and <code>CommonJS</code>. I forgot the original source which influenced me to write the library in this way, but my JavaScript library is written in the following pattern.</p>
  
  <pre><code>(function(window) {
   'use strict';
  
   function getLib() {
    var mylib = {};
    mylib.getSomething = function() {
     return 'x';
    }
    mylib.getCar = function(make, model, year) {
     return {
      make: make,
      model: model,
      year: year
     };
    }
    return mylib;
   }
  
   if(typeof module === 'object' &amp;&amp; module &amp;&amp; typeof module.exports === 'object') {
      module.exports = getLib();
    } else {
      if(typeof(mylib) === 'undefined') {
        window.mylib = getLib();
      }
  
      if(typeof define === 'function' &amp;&amp; define.amd) {
        define('mylib', [], getLib());
      }
    }
  })(this);
  </code></pre>
  
  <p>How do I write a type definition <code>d.ts</code> file for this JavaScript library?</p>
  
  <p>I attempted to write a <code>d.ts</code> as follows, but it does not work. In my Angular app, I put the following file <code>node_modules/@types/mylib/index.d.ts</code>.</p>
  
  <pre><code>interface Car {
   make: string;
   model: string;
   year: number;
  }
  
  interface MyLib {
   getSomething(): string;
   getCar(make: string, model: string, year: number): Car;
  }
  
  declare module "mylib" {
   export let mylib: MyLib
  }
  </code></pre>
  
  <p>In my controller, I simply try to import the library and debug it to the console, but get <code>undefined</code>.</p>
  
  <pre><code>import {mylib} from 'mylib'; //IDE doesn't complain, seems to find the type def
  
  export class MyPage {
   constructor() {
    console.log(mylib); //undefined
    console.log(mylib.getCar('ford', 'f150', 2018)); //code won't reach here
   }
  }
  </code></pre>
  
  <p>Note that the JavaScript package <code>mylib</code> is not on <code>NPM</code> but a private repository and I have already installed it (e.g. <code>npm install mylib --save</code>) to <code>node_modules/mylib</code>. I mention this because I am not sure where the type def file should go. </p>
  
  <p>Any help is appreciated.</p>
  
  <p>UPDATE:</p>
  
  <p>Per the advice below, I've modified my <code>d.ts</code> file to look like the following and placed it in <code>mylib/index.d.ts</code>.</p>
  
  <pre><code>declare module "mylib" {
      let mylib: MyLib;
      export = mylib; 
  }
  </code></pre>
  
  <p>Now, I can import the library in two ways. First way.</p>
  
  <pre><code>import mylib from 'mylib';
  </code></pre>
  
  <p>Second way.</p>
  
  <pre><code>import * as mylib from 'mylib';
  </code></pre>
  
  <p>In VS Code, the IDE, there are no complaints (no red lines hinting at problems). Furthermore, I've modified <code>tsconfig.json</code> to look like the following.</p>
  
  <pre><code>{
    "compileOnSave": false,
    "compilerOptions": {
      "baseUrl": "./",
      "outDir": "./dist/out-tsc",
      "sourceMap": true,
      "declaration": false,
      "module": "commonjs",
      "esModuleInterop": true,
      "moduleResolution": "node",
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "target": "es5",
      "typeRoots": [
        "node_modules/@types"
      ],
      "lib": [
        "es2015.promise",
        "es2017",
        "dom"
      ]
    }
  }
  </code></pre>
  
  <p>When I run <code>ng build</code>, I see the message below.</p>
  
  <pre>
  ERROR in node_modules/typescript/lib/lib.dom.d.ts(3268,88): error TS2344: Type 'SVGElementTagNameMap[K]' does not satisfy the constraint 'Node'.
    Type 'SVGSVGElement | SVGCircleElement | SVGClipPathElement | SVGDefsElement | SVGDescElement | SVGElli...' is not assignable to type 'Node'.
      Type 'SVGFEColorMatrixElement' is not assignable to type 'Node'.
        Types of property 'values' are incompatible.
          Type 'SVGAnimatedNumberList' is not assignable to type 'string[]'.
            Property 'includes' is missing in type 'SVGAnimatedNumberList'.
  node_modules/typescript/lib/lib.dom.d.ts(3626,85): error TS2344: Type 'SVGElementTagNameMap[K]' does not satisfy the constraint 'Node'.
    Type 'SVGSVGElement | SVGCircleElement | SVGClipPathElement | SVGDefsElement | SVGDescElement | SVGElli...' is not assignable to type 'Node'.
      Type 'SVGFEColorMatrixElement' is not assignable to type 'Node'.
  node_modules/typescript/lib/lib.dom.d.ts(10405,11): error TS2430: Interface 'SVGFEColorMatrixElement' incorrectly extends interface 'SVGElement'.
    Types of property 'values' are incompatible.
      Type 'SVGAnimatedNumberList' is not assignable to type 'string[]'.
  node_modules/typescript/lib/lib.dom.d.ts(14172,86): error TS2344: Type 'SVGElementTagNameMap[K]' does not satisfy the constraint 'Node'.
    Type 'SVGSVGElement | SVGCircleElement | SVGClipPathElement | SVGDefsElement | SVGDescElement | SVGElli...' is not assignable to type 'Node'.
      Type 'SVGFEColorMatrixElement' is not assignable to type 'Node'.
  </pre>
  
  <p>The "interesting" thing is that if I comment out the <code>import</code> then run <code>ng build</code> or <code>ng serve</code>, the compile error goes away. While the app is in live reload mode, I can simply uncomment the <code>import</code>, and though the compile error shows up, I can still use all the features of the library in the application. </p>