Question

I'm working at converting an existing codebase to use TypeScript.

Our code includes this hack (it's a workaround for a bootstrap bug):

jQuery().modal.Constructor.prototype.enforceFocus = function () {};

Typescript doesn't like this, and gives this error:

The property 'Constructor' does not exist on value of type '{ (options?: ModalOptions): JQuery; (options?: ModalOptionsBackdropString): JQuery; (command: string): JQuery; }'.

These are the pertinent type definitions (from bootstrap.TypeScript.DefinitelyTyped):

interface JQuery {
    modal(options?: ModalOptions): JQuery;
    modal(options?: ModalOptionsBackdropString): JQuery;
    modal(command: string): JQuery;
}

...but I can't work out how to add to or modify this interface definition to prevent the error.

Was it helpful?

Solution

Unfortunately you cannot define a member var + function with same name. i.e. the following is a compiler error:

interface foo {
    a();
    a:number;
} 

Meanwhile you can do :

(<any>jQuery()).modal.Constructor.prototype.enforceFocus = function () {};

If TypeScript had type unions, which it doesn't (you can vote for these here https://typescript.codeplex.com/workitem/1364) you could potentially do:

interface foo {
    a:()=>void | number;
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top