免责声明:我知道,Parse.com关闭了它的托管服务。不过,我们会继续使用这个架构一段时间,所以这个问题对我们仍然很重要。
最近,我开始使用TypeScript,并认为它可能会大大提高我解析云代码的效率。所以我做了一些测试,并且成功地使用了类型记录来编写云函数等等。我甚至还包括了通过类型进行分析的类型定义。
然而,我仍然不能理解一件事:如何以一种类型安全的方式扩展Parse.Object?
在普通的js中,我会写:
var Foo = Parse.Object.extend("Foo", {
// instance methods
}, {
// static members
});为了确保类型安全,我想在打字稿中写这样的东西:
class Foo extends Parse.Object {
// static members
// instance methods
}这样的事有可能吗?我错过了什么吗?
发布于 2016-06-14 17:44:46
是的,这是可能的。为此,有几个步骤是必要的。首先,需要将类名传递给父构造函数。
class Foo extends Parse.Object {
// static members
// instance methods
constructor() {
// Pass the ClassName to the Parse.Object constructor
super('Foo');
}
}此外,您需要将类注册为Parse对象:
Parse.Object.registerSubclass('Foo', Foo);之后,您只需将其用于查询,如下所示:
var query = new Parse.Query(Foo);
query.find({
success: obj: Foo[] => {
// handle success case
},
error: error => {
// handle error
}
});这也适用于新的解析服务器开放源码项目:https://github.com/ParsePlatform/parse-server。
发布于 2022-10-24 11:45:37
如果像我这样的人偶然发现了同样的问题,下面是我迄今为止的发现:
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;然后,您可以这样使用它:
const newObject = new MyClass({ name: theName, age: theAge });
const result = await newObject.save();
const { attributes } = result;
console.log("Save result", attributes);那么,您将对这些属性具有完全的TS支持。
希望有关https://www.npmjs.com/package/@parse/react的工作能够迅速进行。
https://stackoverflow.com/questions/37644857
复制相似问题