镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Need to support declaration merging in external modules #2821

Closed

Description

@cnshenj

jQuery (jQuery UI) support method extensions. If we write my own jQuery UI widget, I can do this:
In myWidget.ts:

interface JQuery {
    myWidget(): JQuery
}

class myWidget {
    public foo() {
        // This is okay now, but will fail once we add some import statements to this file,
        // because TypeScript thinks this JQuery is not that JQuery
        var some: JQuery = $("#some-id").text();
    }
}

$.fn.extend({
    myWidget() {...}
});

Then in another file:

$(selector).myWidget(); // Create a new myWidget, this is okay because of myWidget() is declared in my version of JQuery interface
$(selector).attr("id", "my-id"); // This is okay because attr is declared in jquery.d.ts

However, this stops working when I add this line to myWidget.ts:

import helpers = require("helpers");

Now TypeScript thinks there 2 JQuery interfaces, instead of merging them into one. How do I tell TypeScript that I'm not creating a new interface for this module, but need to merge it with an existing interface?

Activity

  1. mhegazy commented on Apr 17, 2015

    @mhegazy
    Contributor

    by adding import or export to the top level scope in your file, you are truing this file into a module.

    Modules, as per ES6 spec, have their own scope. so your interface JQuery is a local interface, and does not merge with the global JQuery. TypeScript does not provide a way to model modules affecting global state.

    To do that, you have two options:

    1. Split your file into a .d.ts with the extension to JQuery, and your module would include that .d.ts using ///reference.
    2. do not use modules, i.e. no "imports" and it will be included globally as a script tag

    Please note that ES6 modules have not supported this pattern, but rather a pattern of module composition, where you would import JQuery, add your extension, and export it again. users would only import your module instead of JQuery plus your module. e.g. export * from "JQuery".

  2. cnshenj commented on Apr 20, 2015

    @cnshenj
    Author

    I like the module composition solution. Is it supported by TypeScript yet?

  3. mhegazy commented on Apr 20, 2015

    @mhegazy
    Contributor

    cnshenj ES6 module syntax is supported starting form TypeScript 1.5-alpha

  4. masaeedu commented on Jan 14, 2016

    @masaeedu
    Contributor

    Mohamed Hegazy (@mhegazy)

    Please note that ES6 modules have not supported this pattern, but rather a pattern of module composition, where you would import JQuery, add your extension, and export it again.

    Could you please illustrate this? How would this work when I have two different modules that add different things to JQuery?

  5. mhegazy commented on Jan 14, 2016

    @mhegazy
    Contributor

    See #6213 for the module augmentation support.

  6. 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

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions