title: Writing .d.ts file so I can share constants with JavaScript and TypeScript?
body: <p>I'm working on a node.js project that for historical reasons involves both TypeScript and JavaScript. I'd like to set up some project-wide constants that can be used in both languages. Obvious: write a <code>.js</code> file with the constants, and a <code>.d.ts</code> file that includes the types for TypeScript's use. But no: several hours of beating on the problem (and RTFMing) have gotten me nowhere.</p>
  
  <p>Here's my first try at the constants file:</p>
  
  <pre><code>// constants.js
  const MY_CONST_1 = 1;
  exports.MY_CONST_1 = MY_CONST_1;
  </code></pre>
  
  <p>... and the JavaScript which uses it:</p>
  
  <pre><code>// consumer.js
  let constants = require('./constants');
  let one = constants.MY_CONST_1;
  </code></pre>
  
  <p>That works well, but when I try the obvious in TypeScript:</p>
  
  <pre><code>// consumer.ts
  import * as constants from './constants';
  let one: number = constants.MY_CONST_1;
  </code></pre>
  
  <p>Nope: <code>error TS2307: Cannot find module './constants'.</code> So, gotta write a <code>constants.d.ts</code> file, in the same directory as the <code>constants.js</code> file, right? Sounds easy, but a half dozen different attempts have yielded exactly zilch. No matter what I do, Typescript complains <code>error TS2306: File '/Users/griscom/Documents/Work/test/constants.d.ts' is not a module</code>.</p>
  
  <p>So, what would a successful <code>constants.d.ts</code> file look like? Or, if I'd have to make changes in <code>constants.js</code> for it all to work, what would they be?</p>
  
  <p>P.S. Here's my <code>tsconfig.json</code> file:</p>
  
  <pre><code>{
    "compilerOptions": {
      "target": "es5",
      "module": "commonjs",
      "moduleResolution": "node",
      "sourceMap": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "removeComments": false,
      "noImplicitAny": true,
      "noImplicitReturns": true
    }
  }
  </code></pre>
  
  <p>(This seems like such a simple and obvious need; it's frustrating how opaque the process is.)</p>