最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - (TypeScript) Creating type definition for Parse SDK (Browser and Node) - Stack Overflow

programmeradmin0浏览0评论

I'm creating a Typescript type definition for the Parse Javascript SDK. It's actually plete. 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.

I'm creating a Typescript type definition for the Parse Javascript SDK. It's actually plete. 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.

Share Improve this question edited Sep 3, 2014 at 0:17 Earl Ferguson asked Sep 2, 2014 at 23:58 Earl FergusonEarl Ferguson 9482 gold badges10 silver badges12 bronze badges 8
  • 2 Hi Earl, @basarat has beaten me to it with his good answer below. – Fenton Commented Sep 3, 2014 at 8:21
  • How's the progress on this? Has this been open sourced on GitHub anywhere? – fatuhoku Commented Jun 20, 2015 at 16:54
  • I'm curious about the status of this project as well. – damianesteban Commented Jul 3, 2015 at 3:25
  • @damianesteban Have you guys searched tsd because it's up there. Just do a search for "parse". – Earl Ferguson Commented Jul 4, 2015 at 5:03
  • @ fatuhoku search tsd. – Earl Ferguson Commented Jul 4, 2015 at 5:03
 |  Show 3 more ments

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论