Roger Far
Roger Far

Reputation: 2385

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?

Upvotes: 2

Views: 2622

Answers (3)

stsloth
stsloth

Reputation: 1850

If properly fixing the error or using more decent workarounds like already suggested are not an option, as of TypeScript 2.6 (released on Oct 31, 2017), now there is a way to ignore all errors from a specific line using // @ts-ignore comments before the target line.

The mendtioned documentation is succinct enough, but to recap:

// @ts-ignore
const s : string = false

disables error reporting for this line.

However, this should only be used as a last resort when fixing the error or using hacks like (x as any) is much more trouble than losing all type checking for a line.

As for specifying certain errors, the current (mid-2018) state is discussed here, in Design Meeting Notes (2/16/2018) and further comments, which is basically

"no conclusion yet"

and strong opposition to introducing this fine tuning.

Upvotes: 2

Fenton
Fenton

Reputation: 250892

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.

Upvotes: 1

basarat
basarat

Reputation: 275819

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

UGLY fix use a TypeAssertion:

declare module $data{
    export class EntityContext{}
    export class IPromise<T>{}
    export class EntitySet<T>{}
}
declare module Models{export class UserModel{}}


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>;
    }
}

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

Proper fix

There is a lot wrong with your type definition. You need to review the power of TypeScript interface. Here is a sample to get you started:

interface Container{
    onReady():any;
}
interface ContainerStatic{
    new ():Container;
}

declare var MyEntities:
{
    // A constructor signature
    new (options:any):any; 

    // Sub classes 
    Container:ContainerStatic;
}

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

Upvotes: 2

Related Questions