4

Disclaimer: I know, Parse.com shuts down it's hosted service. Still, we will continue to use the framework for a while, so this question is still important to us.

Recently, I started playing around with TypeScript and figured it might enhance my productivity for parse cloud code a lot. So I did some testing and was successfully able to use typescript to write cloud functions and so on. I even included the typing definition for parse via typings.

However, I still don't get one thing: How can I extend Parse.Object in a type-safe manner?

In normal js I would write:

var Foo = Parse.Object.extend("Foo", {
    // instance methods
}, {
    // static members
});

In order to get type safety, I would like to write something like this in typescript:

class Foo extends Parse.Object {
    // static members
    // instance methods
}

Is something like this possible? Am I missing out on something?

2 Answers 2

8

Yes, this is possible. There are a few steps necessary for this. First, you need to pass the classname to the parent constructor

class Foo extends Parse.Object {
// static members
// instance methods
    constructor() {
        // Pass the ClassName to the Parse.Object constructor
        super('Foo');
    }

}

Furthermore, you need to register your class as an Parse object:

Parse.Object.registerSubclass('Foo', Foo);

After this, you can just use it for your query as:

var query = new Parse.Query(Foo);
query.find({
    success: obj: Foo[] => {
        // handle success case
    },
    error: error => {
        // handle error
    }
});

This also works for the new parse server open source project: https://github.com/ParsePlatform/parse-server

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

2 Comments

Awesome, thanks a lot! I overlooked registerSubclass in the API.
Also, on a side note, one needs to define createWithoutData if one wants to use it: public static createWithoutData(objectId: string): Foo { let newObj = new Foo(); newObj.id = objectId; return newObj; }
0

If someone like my stumble across the same question, here are my findings so far:

import Parse from "parse";

interface IBase {
  readonly createdAt?: Date;
  readonly updatedAt?: Date;
}

interface IMyInterface extends IBase {
  name: string;
  age?: number;
}

class MyClass extends Parse.Object<IMyInterface> {
  constructor(attributes: IMyInterface) {
    super("MyClass", attributes);
  }
}

export default MyClass;

You can then use it like this:

const newObject = new MyClass({ name: theName, age: theAge });

const result = await newObject.save();

const { attributes } = result;

console.log("Save result", attributes);

You will have full TS support for the attributes then.

Hopefully the work on https://www.npmjs.com/package/@parse/react will proceed quickly.

Comments

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.