Skip to content

Extending ambiend modules #5340

New issue

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

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

Already on GitHub? # to your account

Closed
DzonnyDZ opened this issue Oct 20, 2015 · 1 comment
Closed

Extending ambiend modules #5340

DzonnyDZ opened this issue Oct 20, 2015 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@DzonnyDZ
Copy link

Hello, I read #280 & #857 and I'm still confused how can I extend an ambient module. Honestly I'm able to extend it neither nice way nor any other way :-(.
Let's say I have a library I wanna extend. It's not my library, I cannot touch it. It's written in javaScript, so I have .d.ts file for it, e.g.:
AmbientModule.d.ts:

declare module AmbientModule {
     export interface SomeInterface {
        someField?: string;
     }

     export function DoSomething(a: SomeInterface): void;
     export function DoSomethingElse(): void;
}

I wanna add a new field to the SomeInterface interface. Not just some random field, but field of my type. Here's my extension:
Extension.ts:

/// <reference path="AmbientModule.d.ts"/>
//import am = require("AmbientModule");

import SomeInterface = AmbientModule.SomeInterface;

export module Extension {
    export interface ExtData {
        someData?: Number;
    }

    export function MyFunction(): void {
        AmbientModule.DoSomethingElse();
        var x: SomeInterface = { someField: "sf", additionalFiled: { someData: 7 } };
    }
}

declare module AmbientModule {
    export interface SomeInterface {
        additionalFiled?: Extension.ExtData;
    }
}

As you can see I also wanna use the extended interface in the extension itself.
This is the consumer of the extended library:
Consumer.ts:

/// <reference path="AmbientModule.d.ts"/>
//import amb = require("AmbientModule");
import ext = require("Extension");

function main() {
    AmbientModule.DoSomething({
        someField: "aaa",
        additionalFiled: { someData: 7 }
    });
}

For completenes, I have _global.ts
_global.ts:

declare module 'AmbientModule' { }

When I try to compile this I get bunch of errors:

TS2322  Type '{ someField: string; additionalFiled: { someData: number; }; }' is not assignable to type 'SomeInterface'.
  Object literal may only specify known properties, and 'someField' does not exist in type 'SomeInterface'. Extension.ts 13
TS2345  Argument of type '{ someField: string; additionalFiled: { someData: number; }; }' is not assignable to parameter of type 'SomeInterface'.
  Object literal may only specify known properties, and 'additionalFiled' does not exist in type 'SomeInterface'.   Consumer.ts 8
Build: Argument of type '{ someField: string; additionalFiled: { someData: number; }; }' is not assignable to parameter of type 'SomeInterface'.    Consumer.ts 8   
Build: Type '{ someField: string; additionalFiled: { someData: number; }; }' is not assignable to type 'SomeInterface'. Extension.ts    13  

So, either I'm doing something wring, or this IMO important scenario is not supported.

If, you ask why am I doing this. I'm trying to extend the jQuery DataTables library with custom column filtering. For that I need to extend the ColumnSettings interface to add my own per-column configuration.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 21, 2015

looks like a duplicate of: #4166

This is not doable at the moment. we understand this is an important scenario and we are looking into supporting it. see 10/16/2015 design meeting notes for more information.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Oct 21, 2015
@mhegazy mhegazy closed this as completed Oct 21, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants