Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jbrantly opened this issue Feb 12, 2015 · 10 comments
Closed

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

jbrantly opened this issue Feb 12, 2015 · 10 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@jbrantly
Copy link

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?

@danquirk
Copy link
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.

@jbrantly
Copy link
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?

@mhegazy
Copy link
Contributor

mhegazy commented Feb 23, 2015

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

  module myLib {
    export function doSomethingElse(): void;
  }

@jbrantly
Copy link
Author

@mhegazy Adding export did not change the output in my test.

@mhegazy mhegazy added the Bug A bug in TypeScript label Feb 23, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Feb 23, 2015

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;
}

@jbrantly
Copy link
Author

Ah! Very good, thanks!

@danquirk
Copy link
Member

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

@danquirk danquirk reopened this Feb 23, 2015
@jbrantly
Copy link
Author

Ah right, sorry about that.

@mhegazy mhegazy added this to the TypeScript 1.5 milestone Feb 24, 2015
@mhegazy mhegazy assigned yuit and unassigned sheetalkamat May 25, 2015
@mhegazy mhegazy modified the milestones: TypeScript 1.7, TypeScript 1.6 Jul 1, 2015
@jbrantly
Copy link
Author

jbrantly commented Aug 5, 2015

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"

@mhegazy
Copy link
Contributor

mhegazy commented Aug 5, 2015

thanks @jbrantly for reporting back. resolving.

@mhegazy mhegazy closed this as completed Aug 5, 2015
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Aug 5, 2015
@mhegazy mhegazy modified the milestones: TypeScript 1.6, TypeScript 1.7 Aug 5, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants