1

I am trying to create a typescript definition file for an existing javaScript library, but keep getting run time errors.

The library contains the following code:

/**
* Pdok namespace, will hold Api namespace
* @namespace
*/
Pdok = {};

Pdok.Api = function(config) {

/* implementation details are here... */
}

The javascript example code that was supplied works and contains the following line:

var api = new Pdok.Api(config);

I created a typescript definition file, and am able to compile without any problems. The definition looks like:

declare namespace Pdok
{
    interface configOptions {
        /* .. details .. */
    }

    class Api {
        constructor(config: configOptions);
    }
}

At run time, i get the following error: Uncaught TypeError: Pdok.Api is not a constructor

What should be in my .d.ts-file to get this working?

2
  • Curiously, their example shows usage as api = Pdok.Api(config) (no use of new) yet the Api function does look like a constructor (uses this a bunch and does not explicitly return anything). Commented May 9, 2016 at 15:57
  • Yes, i also noticed the code in the comments was different from the examples available through pdok.nl/sites/default/files/pdokkaart/…. The examples from these files are all working. This is also the source i used for the pdok-api.js. Commented May 9, 2016 at 17:24

1 Answer 1

0

Based on this answer https://stackoverflow.com/a/15008808/4516689 it could be that the source isn't loaded correctly.

Set a debugger; directive before your code and check to what the variable Pdok.Api is set to.

debugger;
var api = new Pdok.Api(config);

or use this

alert(Pdok.Api);
var api = new Pdok.Api(config);

it should return "function (config) { ..."

Sign up to request clarification or add additional context in comments.

3 Comments

You were right. The issue was that the scripts were not loaded in the correct order.
If you create your own .d.ts maybe this link can help you. github.com/fivetran/typescript-closure-tools It creates .d.ts from JSDoc. Not perfect but as base for your on defintion file.
Thanks for the link. I will look into this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.