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

Unable to do module augmentation #7148

Closed
yortus opened this issue Feb 19, 2016 · 13 comments
Closed

Unable to do module augmentation #7148

yortus opened this issue Feb 19, 2016 · 13 comments
Labels
Bug A bug in TypeScript

Comments

@yortus
Copy link
Contributor

yortus commented Feb 19, 2016

TypeScript Version:

nightly (1.9.0-dev.20160217)

Code:

// OK - fixed by #6742
declare module 'express' {
    interface Request {
        id: number;
    }
}

// ERROR: Cannot augment module 'knockout' because it resolves to a non-module entity
declare module 'knockout' {
    interface KnockoutStatic {
        track(obj: any, propertyNames?: string[]): any;
        mapping: KnockoutMapping;
    }
}

// ERROR: Cannot augment module 'jquery' because it resolves to a non-module entity
declare module 'jquery' {
    interface JQuery {
        timepicker(): JQuery;
        timepicker(options: TimePickerOptions): JQuery;
    }
}

Asking the same thing as #6722, but for jquery and knockout.

@DanielRosenwasser
Copy link
Member

I'm assuming that the relevant .d.ts files are part of your compilation, right? Do you have a minimal repro?

@yortus
Copy link
Contributor Author

yortus commented Feb 19, 2016

@DanielRosenwasser here's a little repro project: https://github.com/yortus/repro-7148

@Guria
Copy link

Guria commented Mar 16, 2016

I have failed to augment moment as well

@vladima
Copy link
Contributor

vladima commented Mar 16, 2016

discussed offline with @mhegazy: this is not a bug in implementation but rather the consequence of the fact that currently typings for 'knockout' and 'jquery' are not augmentation-friendly - they both export variable with some global type as module and so i.e. in case of JQuery

declare module 'jquery' {
    interface JQuery {
        timepicker(): JQuery;
        timepicker(options: TimePickerOptions): JQuery;
    }
}

module 'jquery' is defined as

declare module "jquery" {
    export = $;
}
declare var jQuery: JQueryStatic;
declare var $: JQueryStatic;

so type of 'jquery' is effectively JQueryStatic that does not have nested interface JQuery thus the failure.

What you can do:

  • UMD modules support that will be shipped TS 2.0 will support this scenario
  • for now since JQueryStatic is defined in a global scope you can use global augmentation to patch it.
/// <reference path="./jquery.d.ts" />
declare global {
    interface JQuery {
        timepicker(): JQuery;
    }
}

$().timepicker();

export {}

@cervengoc
Copy link

@vladima Could you please add a few words about how the above mentioned UMD support solves this? I'm currently stuck with this situation and wouldn't like to use the global scope declarations unless I have no other way.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 4, 2017

@vladima Could you please add a few words about how the above mentioned UMD support solves this? I'm currently stuck with this situation and wouldn't like to use the global scope declarations unless I have no other way.

http://www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html#umd does not cover your question?

@cervengoc
Copy link

It doesn't get me closer, as I still don't see what the working alternative is for

declare module 'jquery' {
    interface JQuery {
        timepicker(): JQuery;
        timepicker(options: TimePickerOptions): JQuery;
    }
}

as it still doesn't work at me. The compiler still can't see these extension methods.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 4, 2017

where is jquery defined?

@cervengoc
Copy link

It's installed via the @types/jquery NPM package.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 4, 2017

the issue is that Jquery as defined in DefinitelyTyped is not a module, it is really a variable.

The correct fix is to submit a PR to DefinitelyTyped turning the jquery definition into a module.

Alternatively, you can augment the interface JQuery, e.g.:

declare global {
    interface JQuery {
        timepicker(): JQuery;
        timepicker(options: TimePickerOptions): JQuery;
    }
}

@cervengoc
Copy link

Yes I know that, I was just confused because @vladima wrote in one of the previous posts that there are two options, and the first option was to wait for UMD support as it will solve it. The second was to augment the global scope jQuery definitions.

So basically UMD support doesn't solve this particular issue, as the issue is still with the jQuery definition file if I understand correctly. Thank you for the clarification.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 4, 2017

well, jquery does not use UMD modules. so no, it does not solve the issue for jquery.

@deadmann
Copy link

deadmann commented Jul 14, 2017

Severity Code Description Project File Line Suppression State
Error Build:Cannot augment module 'moment' because it resolves to a non-module entity. RealEstate F:\Project2017\S1\BaseProject\RealEstate\RealEstate\Scripts\libs\typings\modules\moment-jalaali\index.d.ts 13

// Generated by typings
// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/e1b64bbf24fed6d801c9f56e8a041794c4417eca/moment-jalaali/index.d.ts
declare module 'moment-jalaali' {
// Type definitions for moment-jalaali 0.5.0
// Project: https://github.com/jalaali/moment-jalaali
// Definitions by: Ali Taheri Moghaddar <https://github.com/alitaheri>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

import * as moment from 'moment';

export = moment;

module 'moment' {
    type JUnitOfTime = 'jYear' | 'jMonth';

    /**
     * Add persian language.
     */
    function loadPersian(): void;

    function jIsLeapYear(year: number): boolean;
    function jDaysInMonth(year: number, month: number): number;

    interface Moment {

        startOf(jUnitOfTime: JUnitOfTime): Moment;
        endOf(jUnitOfTime: JUnitOfTime): Moment;

        add(amount: string | number, jUnitOfTime: JUnitOfTime): Moment;

        subtract(amount: string | number, jUnitOfTime: JUnitOfTime): Moment;

        jYear(y: number): Moment;
        jYear(): number;
        jMonth(M: number | string): Moment;
        jMonth(): number;
        jDate(d: number): Moment;
        jDate(): number;
        jWeek(d: number): Moment;
        jWeek(): number;
        jWeekYear(d: number): Moment;
        jWeekYear(): number;
        jDayOfYear(d: number): Moment;
        jDayOfYear(): number;
    }

}
}

i have same issue.

@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
Projects
None yet
Development

No branches or pull requests

7 participants