Search code examples
typescriptjaydata

Ignore typescript definition error


I have a 3rd party typescript definition file (JayData):

declare module MyEntities
{
    export class Container extends $data.EntityContext
    {
        public onReady(): $data.IPromise<any>;
        public onReady(handler: (context: Container) => void): $data.IPromise<any>;

        public Users: $data.EntitySet<Models.UserModel>;
    }
}

But a valid piece of Javascript is how to initialize the MyEntities class:

var db = new MyEntities({ name: 'local', databaseName: 'MyDb' });

But for TS this doesn't make sense, MyEntities is a module and not a class, thus throwing the compile error: Cannot invoke an expression whose type lacks a call signature.

Is there any way around this to ignore this compile error?


Solution

  • As you are dealing with an auto-generated defintion, you may find this trick useful.

    You can use module-merging to solve your issue without editing the auto-generated file.

    Include this definition file first and it will merge into the auto-generated file to create a merged "class module". (With definition files, the order isn't as important as if you are trying to use module-merging in real implementation code).

    myextension.d.ts

    declare class MyEntities {
        constructor(options: { name: string; databaseName: string; })
    }
    

    Both declarations must have the same common root (this is what will cause them to merge), but looking at the definition in your question it looks like it will work for you.