Skip to content

Merge and re-export module in external module declaration #2019

Description

Let's say I'm writing a declaration file for a library that exports two external modules: myLib and myLibWithExtras. The myLibWithExtras module is exactly myLib but with some additional items.

As an example of what I'm trying to do:

// myLib.d.ts
declare module "myLib" {
  function doSomething(): void;
}

declare module "myLibWithExtras" {
  // this doesn't work 
  import myLib= require("myLib");
  module myLib {
    function doSomethingElse(): void;
  }
  export = myLib;
}

// consumer.ts
import myLib = require("myLib")
import myLibWithExtras = require("myLibWithExtras")
myLib.doSomething();
myLib.doSomethingElse(); // should not compile
myLibWithExtras.doSomething();
myLibWithExtras.doSomethingElse();

Is there any way to accomplish this?

Activity

  1. danquirk commented on Feb 12, 2015

    @danquirk
    Member

    You need a /// reference to myLib.d.ts in consumer.ts so that it knows where to find the quoted names (myLib, myLibWithExtras). Otherwise this is all correct and works for me with the latest bits.

  2. jbrantly commented on Feb 13, 2015

    @jbrantly
    Author

    Hmm. I built off master and modified consumer.ts and got this:

    /// <reference path="myLib.d.ts" />
    
    import myLib = require("myLib")
    import myLibWithExtras = require("myLibWithExtras")
    myLib.doSomething();
    //myLib.doSomethingElse(); // should not compile
    myLibWithExtras.doSomething();
    myLibWithExtras.doSomethingElse(); // should compile but doesnt
    
    ~/tstest$ node ../TypeScript/built/local/tsc.js consumer.ts --module commonjs
    consumer.ts(8,17): error TS2339: Property 'doSomethingElse' does not exist on type 'typeof "myLib"'.
    

    Perhaps I'm missing something simple?

  3. mhegazy commented on Feb 23, 2015

    @mhegazy
    Contributor

    Your doSomethingElse function is not exported in the d.ts file:

      module myLib {
        export function doSomethingElse(): void;
      }
  4. jbrantly commented on Feb 23, 2015

    @jbrantly
    Author

    Mohamed Hegazy (@mhegazy) Adding export did not change the output in my test.

  5. mhegazy commented on Feb 23, 2015

    @mhegazy
    Contributor

    Oh.. my bad. this is actually a bug, import declarations can not merge with other declarations. The bug is to get an error message in mylib.d.ts to not export the import but complain that it is merged with the module. The compiler shows the error correctly in non-ambient declarations but not here.

    I think your scenario would work well with the new import syntax:#1983, this will be in our next release (1.5).

    so your code will look like:

    // myLib.d.ts
    declare module "myLib" {
      export function doSomething(): void;
    }
    
    declare module "myLibWithExtras" {
      export * from "myLib";
      export function doSomethingElse(): void;
    }
  6. jbrantly commented on Feb 23, 2015

    @jbrantly
    Author

    Ah! Very good, thanks!

  7. danquirk commented on Feb 23, 2015

    @danquirk
    Member

    Re-opening, sounds like we do want to fix today's error message.

  8. jbrantly commented on Feb 23, 2015

    @jbrantly
    Author

    Ah right, sorry about that.

  9. jbrantly commented on Aug 5, 2015

    @jbrantly
    Author

    I think this might be fixed.

    The following code:

    declare module "myLib" {
      function doSomething(): void;
    }
    
    declare module "myLibWithExtras" {
      // this doesn't work 
      import myLib= require("myLib");
      module myLib {
        function doSomethingElse(): void;
      }
      export = myLib;
    }

    now throws the following error: "Import declaration conflicts with local declaration of 'myLib' module"

  10. mhegazy commented on Aug 5, 2015

    @mhegazy
    Contributor

    thanks James Brantly (@jbrantly) for reporting back. resolving.

  11. locked and limited conversation to collaborators on Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions