Skip to content
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

Module Augmentation fails when exports are declared in an ExportClause #8427

Closed
EvanCahill opened this issue May 2, 2016 · 16 comments
Closed
Labels
Bug A bug in TypeScript Duplicate An existing issue was already created

Comments

@EvanCahill
Copy link
Member

TypeScript Version:

nightly (1.9.0-dev.20160502)

Code

// ext1.d.ts
declare module "State" {
    interface IState {
        num: number;
    }
    export { IState };
}

// ext2.d.ts
declare module "State" {
    interface IState {
        str: string;
    }
    export { IState };
}

//app.ts
import { IState } from "State";

function test(state: IState): void {
    console.log(state.num);
    console.log(state.str);
}

// tsconfig.json
{
    "files": [
        "app.ts",
        "ext1.d.ts",
        "ext2.d.ts"
    ]
}

Expected behavior:
The "State" module and IState interface declarations should be merged and the code should compile without errors.

Actual behavior:
The declaration files report error TS2300: Duplicate identifier 'IState'

Workaround:
The above errors do not occur when both IState interfaces are exported inline using the export modifier instead of in a ExportClause.
NOTE: If the first is exported inline and the second is exported in an ExportClause then the second declaration errors with TS2482: Export declaration conflicts with exported declaration of 'IState'.

@mhegazy
Copy link
Contributor

mhegazy commented May 2, 2016

The behavior is what i would expect. the error message could be better.

also note, you do not need the export modifier. all declarations are implicitly exported in an ambient context. so this is is semantically equivalent to your sample:

// ext2.d.ts
declare module "State" {
    interface IState {
        str: string;
    }
}

@mhegazy mhegazy added Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging labels May 2, 2016
@mhegazy mhegazy added this to the Community milestone May 2, 2016
@mhegazy mhegazy added the Help Wanted You can do this label May 2, 2016
@EvanCahill
Copy link
Member Author

If the two are semantically equivalent then why does the the former producer errors while the later does not? That is, if both interface declarations omit the export and are implicitly exported instead then the code compiles.

With regards to the use of the export keyword I included it as that is how the declarations are generated by the compiler when compiling with --module system|amd, --outFile and --declaration. For a little more context on the problem I have 3 projects all setup with the above compiler options. The first two projects both have a State.ts file which declare the IState interface. The third project consumes the other two. If I export the IState interface in both places using an inline export then everything compiles and works as desired. If instead I export them in an ExportClause then I get the duplicate identifier errors.

@Denis535
Copy link

It seems I also stumbled on this problem.

import gulp = require("gulp"); // import declaration conflicts with local declaration of gulp

gulp["devTask"] = function () {
    if (process.env.NODE_ENV !== 'production') return gulp.task.apply(this, arguments);
}

module gulp {
    interface Gulp {
        devTask(name: string): void;
    }
}

I don't understand is this error a bug or not?
If not, why I can merge interfaces in my modules, but can't merge with not mine module?
What does Domain: Error Messages mean? As I understand you just want to replace error text?

@mhegazy
Copy link
Contributor

mhegazy commented May 21, 2016

looking at this again, this is the same issue as #8140.

@mhegazy
Copy link
Contributor

mhegazy commented May 21, 2016

closing in favor of #8140.

@mhegazy mhegazy closed this as completed May 21, 2016
@mhegazy mhegazy added Duplicate An existing issue was already created and removed Help Wanted You can do this Domain: Error Messages The issue relates to error messaging labels May 21, 2016
@mhegazy mhegazy removed this from the Community milestone May 21, 2016
@Denis535
Copy link

All the same I don't understand. Should it work or not?
Now I changed variable name:

import _gulp = require("gulp");

_gulp["devTask"] = function () {
    if (process.env.NODE_ENV !== 'production') return _gulp.task.apply(this, arguments);
}

declare module gulp {
    interface Gulp {
        devTask(name: string): void;
    }
}

Now there is no errors, but _gulp still hasn't my function.

@mhegazy
Copy link
Contributor

mhegazy commented May 23, 2016

All the same I don't understand. Should it work or not?

I think it should not work, and it should be an error. currently it is not an error, but it does not work as expected.

Now there is no errors, but _gulp still hasn't my function.

you should use module augmentation instead:

import gulp = require("gulp");

// augment the module
declare module "gulp" {
    interface Gulp {
        devTask(name: string): void;
    }
}

gulp.devTask = function () {
    if (process.env.NODE_ENV !== 'production') return gulp.task.apply(this, arguments);
}

@Denis535
Copy link

Denis535 commented May 23, 2016

You code throws an error.
declare module "gulp" { // duplicate identifier ' "gulp" '

@mhegazy
Copy link
Contributor

mhegazy commented May 23, 2016

what version of TS are you using?

@Denis535
Copy link

Typescript for Microsoft Visual Studio 1.8.29.0.

@mhegazy
Copy link
Contributor

mhegazy commented May 23, 2016

looks like you are running into #7545, which has been fixed after 1.8.

@Denis535
Copy link

Denis535 commented May 23, 2016

What is the last version?
On npm I see 1.8.10. On this repository I see 1.9.0.
Here is https://www.microsoft.com/en-us/download/details.aspx?id=48593 even version 1.8.6.0 (But my currently installed one is 1.8.29.0).
In C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.8\ tsc version 1.8.9.

Is there a way to get version of TypeScript used for compiling? Because I'm not sure what TS version VS is using.

@mhegazy
Copy link
Contributor

mhegazy commented May 23, 2016

tsc --v

What is available currently is the nightly builds (npm install typescript@next).

@Denis535
Copy link

Denis535 commented May 23, 2016

tsc --v

I mean something kind of
console.log( TypeScript.version );
Because I'm not sure which the TS is used (npm loca, global, Microsoft SDKs).

Ok, I installed typescript@next. And read this https://github.com/Microsoft/TypeScript/wiki/Dev-Mode-in-Visual-Studio
Do I really must to edit the registry? Why not make just TS path option in Visual Studio?

@mhegazy
Copy link
Contributor

mhegazy commented May 23, 2016

VS stores options in the registry as well :)

@shaunluttin
Copy link

shaunluttin commented Aug 23, 2017

We're seeing a similar error. After doing npm install ag-grid --save, we tried to augment the module as follows.

// augmentation.d.ts
import agGrid = require("ag-grid");

declare module "ag-grid" {
    interface ColDef {
        str: string;
    }
}

On build with tsc 2.4.1 we see the following error:

node_modules/ag-grid/main.d.ts(135,10): error TS2484: Export declaration conflicts with exported declaration of 'ColDef'.

We have also tried this approach:

// augmentation.d.ts
declare module "ag-grid" {
    interface ColDef {
        str: string;
    }
}

In that case, the actual behavior is that the ColDef interface now has only the str property.

// index.ts
import { ColDef } from "ag-grid";

let def: ColDef;

image

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants