Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(TypeScript) Creating type definition for Parse SDK (Browser and Node)

I'm creating a Typescript type definition for the Parse Javascript SDK. It's actually complete. The definition would work for the Browse and Cloud Code but not in Node.

Using Parse with both client side and server side javascript

Browse Example:

var GameScore = Parse.Object.extend("GameScore");

Node Example:

var Parse = require('parse').Parse;
var GameScore = Parse.Object.extend("GameScore"); 

Psuedue Type Definition

declare module Parse {

    interface FacebookUtils {
        init(options?: any);
    }

    class Object {
        constructor(attributes?: any, options?: any);

    }

    module Cloud {

        interface FunctionResponse {
            success?: (response: HttpResponse) => void;
            error?: (response: HttpResponse) => void;
        }
    }

}

declare module "parse" {
    export = Parse;
}

Using Parse with newly create definition

class GameScore extends Parse.Object {}

var gameScore = new GameScore();

Everything would work fine in the Browse because the Parse library is globally available.

Now in Node everything would work fine if the import were like this:

var Parse = require("parse"); 

But the import is actually like this:

var Parse = require("parse").Parse;

So the ".Parse" at the end of the import would cause a problem when trying to access Parse.Object because there is no accessor for ".Parse" in the definition above.

So what I want to do is have a single definition file that works for both Browser and Node. I thought about using an interface but I don't think that would allow me to do extends or implements when I want to.

For example:

declare module Parse {

    ...

    interface Parse {
        Object: Object;
    }
}

declare module "parse" {
        export = Parse;
}

This wouldn't allow me to extend Object, it's just defined as a property in the Parse interface.

Any help would be great. Thanks.

like image 635
Earl Ferguson Avatar asked Sep 02 '14 23:09

Earl Ferguson


1 Answers

It is a bit involved :

declare module Parse {

    interface FacebookUtils {
        init(options?: any);
    }

    class Object {
        constructor(attributes?: any, options?: any);

    }

    module Cloud {

        interface FunctionResponse {
            success?: (response: HttpResponse) => void;
            error?: (response: HttpResponse) => void;
        }
    }

}

declare module "parse" {
    var captureType: typeof Parse;
    var subType: {
        Parse: typeof captureType;
    }
    export = subType;
}

Usage:

///<reference path="parse.d.ts"/>

import parse = require('parse');
var Parse = parse.Parse;

Explanation

To refer to the type of a module you need typeof. Knowing this, you might want to do the following but can't for obvious reason (which Parse is this? ... the global one? or local?):

var Parse: { Parse: typeof Parse }
export = Parse;

Hence the verbosity of local Types. Don't worry they do not bloat the generated JavaScript in any way.

like image 93
basarat Avatar answered Oct 27 '22 19:10

basarat