Announcement

Collapse
No announcement yet.

How to import Typescript declarations into separate Typescript project

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    How to import Typescript declarations into separate Typescript project

    I've created a Bridge.net 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 Bridge.net 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 Bridge.net 17.7.0 and my Typescript project is on 3.1.

    My Bridge project is setup like this:

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

    out/foo.bar.d.ts
     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/foo.bar.d.ts' is not a module.
    Could someone please offer some direction?
    Last edited by Jimmy; 2019-04-09 @ 11:25 PM.

    #2
    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!

    Comment


      #3
      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 @ 04:33 PM.

      Comment

      Working...
      X