(TypeScript) Create a type definition for the Parse SDK (browser and Node)

I am creating a Typescript type definition for the Parse Javascript SDK . It is truly complete. The definition will work for review code and the cloud, but not in Node.

Using Parse on both sides on the client side and on the server side javascript

Search example:

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

Node Example:

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

Psueue 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 New Definition Creation

 class GameScore extends Parse.Object {} var gameScore = new GameScore(); 

Everything will work well in Overview , because the Parse library is available worldwide.

Now in Node, everything will work fine if the import was like this:

 var Parse = require("parse"); 

But the import actually looks like this:

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

So, β€œ. Parse” at the end of the import may cause a problem when trying to access Parse.Object , because there is no access for β€œ.Parse” in the above definition.

So what I want to do is have one definition file that works for both the browser and Node. I was thinking about using an interface, but I don’t think that it will allow me to do extensions or implement when I want.

For instance:

 declare module Parse { ... interface Parse { Object: Object; } } declare module "parse" { export = Parse; } 

This would not allow me to extend Object , it is simply defined as a property in the Parse interface.

Any help would be great. Thanks.

+5
source share
1 answer

This is a bit related:

 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; } 

Using:

 ///<reference path="parse.d.ts"/> import parse = require('parse'); var Parse = parse.Parse; 

Explanation

To refer to the type of module, you need typeof . Knowing this, you can do the following, but you cannot for an obvious reason (what is Parse ? Global or local?):

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

Therefore, the verbosity of local types. Do not worry, they do not inflate the generated JavaScript in any way.

+6
source

Source: https://habr.com/ru/post/1201682/


All Articles