No announcement yet.

How to import Typescript declarations into separate Typescript project

  • Filter
  • Time
  • Show
Clear All
new posts

    How to import Typescript declarations into separate Typescript project

    I've created a project that I'm publishing to NPM as a module and everything's worked quite well for me using plain Javascript so far. However, one of the projects I have consuming my module is in Typescript and I'd like to make use of the Typescript declaration files generated by the Bridge. Yet, I cannot for the life of me figure out how one is actually meant to accomplish this, as I don't have much experience using declaration files that aren't part of DefinitelyTyped.

    If you need more information just let me know. I'm using 17.7.0 and my Typescript project is on 3.1.

    My Bridge project is setup like this:

    "generateTypeScript": true, "module": { "name": "@foo/bar", "type": "UMD" } }
     "name": "@foo/bar", "main": "out/", "types": "out/", ...  
    namespace Foo.Bar {
        public static class FooBar {}
    When I build the project the type declarations are generated and appear to be correct:

     declare namespace _foo_bar.Foo.Bar { ... }    
     declare module "_foo_bar" {     export = _foo_bar; }

    In the consuming Typescript project I was previously just requiring the package and everything worked at runtime:
    const FooBarModule = require('@foo/bar');
    but now I want Typescript validation for all of this and I thought the simplest way would be to replace require with import:
    import * as FooBarModule from '@foo/bar';
    But this results in a compilation error:
    error TS2306: File '.../node_modules/@foo/bar/out/' is not a module.
    Could someone please offer some direction?
    Last edited by Jimmy; 2019-04-10 @ 12:25 AM.

    Hello Jimmy!

    I believe if you want to use typescript like that you'd need to also imbue the modules concept to your project. As far as I could read in a stackoverflow thread involving the error you're getting, the looks of it suggests you want the ES6 module outline.

    Hope this helps!


      Thanks for taking the time to help me Fabricio! I tried what you suggested. I put Module attributes in, trying both CommonJS and ES6 ModuleTypes. ES6 module attributes cause an error:

      Unexpected token export
      because the compiled Javascript has the export statement inside of a function call, but, at least in the Node 8.10 runtime that I'm using, exports can only be done at the top level (or so I thought). Furthermore, if I toggle generateTypescript to false in bridge.json and leave everything else alone in the example I initially posted, the other project in which I'm using Typescript can use the ES6 syntax to import the module just fine. I therefore don't believe the problem I mentioned in the original post is caused by the generated Javascript, but rather the declaration files for Typescript specifically. Do you know off hand what version of TS those declaration files are meant to support?
      Last edited by Jimmy; 2019-04-12 @ 05:33 PM.