stopyoukid
stopyoukid

Reputation: 182

Extending modules with a different name

We are in the process of converting some of our javascript to be typescript. We have some existing functionality that we have pulled out into another javascript library that is maintained by us, for example:

Framework.utils = {
    utilFnOne: function ()...,
    utilFnTwo: function ()...,
    utilFnThree: function ()...,
}

We also have some utilities that we use in our non framework code that is like this:

Current.utils = Framework.utils;  // Extend the framework's utils
Current.utils.utilFnFour = function()...;
Current.utils.utilFnFive = function()...;

Typescript

We can create definitions for the framework library as follows:

declare module Framework.utils {
    export function utilFnOne();
    export function utilFnTwo();
    export function utilFnThree();
}

And we can create a typescript definition for Current.utils by doing something like:

module Current.utils {
     export function utilFnFour() {}
     export function utilFnFive() {}
     export class StringUtils { }
}

It would be nice if we could do something like this:

module Current.utils extends Framework.utils { .... }

So that intellisense would show members from both Framework and Current when we do

Current.utils.  

Edit - Resolution

I took the idea that Ryan had and modified it slightly

Framework.d.ts

declare module Framework {
    export var util: _utilImpl;
    export class _utilImpl {
        utilFnOne(a: string) : string;
    }
}

Current.ts

module Current {
    export var util = new _utilImpl();
    export class _utilImpl extends Framework._utilImpl {
        utilFnTwo(b: number) : number;
    }
}

Upvotes: 0

Views: 1013

Answers (1)

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 221392

You can refactor this a bit using interface instead of module to get the desired behavior:

interface FrameworkUtils {
    utilFnOne(s: string);
    utilFnTwo(s: string);
}

declare module Framework {
    export var utils: FrameworkUtils;   
}

Framework.utils.utilFnOne('');

/// ... later

interface CurrentUtils extends FrameworkUtils {
    utilFnFour();
}

declare module Current {
    export var utils: CurrentUtils; 
}

Current.utils.utilFnOne('');
Current.utils.utilFnFour();

Upvotes: 1

Related Questions