JuKe
JuKe

Reputation: 673

type definition for third party class

i have a third party library called canJS. there is this can.Map module. this module is written in AMD. you can load that with a dependency loader e.g. requirejs.

with can.Map we provide 2 ways for create an instance.

  1. let foo = new can.Map([props])
  2. let Foo = can.Map.extend([name,] [staticProperties,] instanceProperties); let foobar = new Foo();

i tried to write a simple type definition for this class

declare module "can/map/" {
  class canMap {
  }
}

but now i stuck.

how look that kind of type defintion. i always get an error like this:

Cannot use 'new' with an expression whose type lacks a call or construct signature

Upvotes: 2

Views: 3067

Answers (1)

TSV
TSV

Reputation: 7641

You can use appropriate documentation to write a declaration file.

In your case, I think, it will be something like this:

declare module can {
  class Map {
    constructor(props: any);
    static extend(name, staticProperties, instanceProperties): Map;
    attr(): {[index: string]: any};

    // Declarations for other properties and functions
  }
}

usage (of course, "canjs" should be loaded):

var a = new can.Map({});
var b = can.Map.extend({}, {}, {});
var c = a.attr();

Update 1

The best way is find definitely typed declaration on definitelytyped.org. Almost all popular libraries definitions already have been written.

You can use typings for "d.ts" files management.

Update 2

Optional arguments ("?" marked) and static function "extend" returns a constructor (with overload):

declare module can {
  class Map {
    constructor(props?: any);
    static extend(instanceProperties): { new(): Map };
    static extend(name, staticProperties, instanceProperties): { new(): Map };
    attr(): {[index: string]: any};

    // Declarations for other properties and functions
  }
}

let creator1 = can.Map.extend({});
let creator2 = can.Map.extend({}, {}, {});
var c = new creator1();

Of course, you should specify type of function arguments (now thay are of "any" type). You can find more details in Typescript documentation.

Upvotes: 2

Related Questions