Weblurk
Weblurk

Reputation: 6802

How do I use Typescript instead of Javascript for writing cloud code in parse-server?

I'm writing a web app using Angular 10 in the frontend and Parse-Server in the backend.

I would like to use Typescript instead of Javascript for writing all my cloud code functions and jobs, but I'm a bit unsure how to set it up, in Angular everything is "natively" Typescript so no set up needed there.

There's remarkable little information out there on how to set up Typescript with Parse. All I found was this incomplete medium article: https://medium.com/@avifatal/using-parseplatform-cloud-code-with-typescript-d27ded2e5054

How do I set up Typescript instead of Javascript for writing cloud code in parse-server?

Upvotes: 2

Views: 2686

Answers (2)

Vesi Staneva
Vesi Staneva

Reputation: 11

What I can tell you from the experience I have with our customers using Typescript on SashiDo is that since Typescript is a superset of JavaScript that is compiled to JavaScript, you can definitely use it in Cloud Code. You'll need to supply the compiled JavaScript files to be load in the cloud code as regular JS files.

You do not need to require Parse JS SDK as it is exposed as a global variable to make it easier to use and initialize it for you.

Please note that this would work only on parse server version 2.8.4 and above.

You can also put the build on postinstall and your code needs to go in the cloud directory and you need to have a main.js in there. This way you can store the .ts file on the repo and ignore the js files.

Here is an example for postinstall: "postinstall": "npm run build"

Upvotes: 0

vegidio
vegidio

Reputation: 912

I'm not familiar with Angular, but I wrote many projects in TypeScript that use both Parse Server and the Parse SDK for JavaScript.

The Parse Server itself don't have any type definitions, so in order to use it in your project you must make sure to create a type definition file somewhere in your project folder (index.d.ts) and declare the module to the Parse Server, like this:

declare module 'parse-server'

But don't expect intelligent code completion in this case.

But if you are using the Parse SDK for JS then everything is much easier because you just need to import the package @types/parse in your package.json (alongside with the parse package) and everything should work out of the box in your TypeScript code.

If you need an example, I use cloud code in my Parse Server in that you can find in this repo. Look at the folder src/cloud.ts.

Upvotes: 4

Related Questions