5

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.

8
  • 2
    Hi Earl, @basarat has beaten me to it with his good answer below. Commented Sep 3, 2014 at 8:21
  • How's the progress on this? Has this been open sourced on GitHub anywhere? Commented Jun 20, 2015 at 16:54
  • I'm curious about the status of this project as well. Commented Jul 3, 2015 at 3:25
  • @damianesteban Have you guys searched tsd because it's up there. Just do a search for "parse". Commented Jul 4, 2015 at 5:03
  • @ fatuhoku search tsd. Commented Jul 4, 2015 at 5:03

1 Answer 1

6

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.

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

1 Comment

Awesome dude!!!! And so much clearer than I thought it would. Pretty darn cool. Thanks man!!!!

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.