我使用angularjs2 + Meteor进行开发。这里的问题是我以下列风格写作模型 -
export interface temp {
name: string;
count: number;
}
问题在于,我使用typescript进行文件扩展,因此在编译后将其转换为原始文本。所以这个模型不安全。用户可以插入任何类型的数据,这是主要问题。
在meteor + reactJs应用程序中,我使用了Meteor.collection,它提供了安全性。但是在这里我们如何才能使打字稿中的模型更加安全?
答案 0 :(得分:1)
TypeScript不提供运行时类型检查。你必须自己写。
ensureArgIsTemp(arg: temp): arg is temp {
if (!arg
|| Object.keys(arg).length !== 2
|| typeof arg.name !== "string"
|| typeof arg.count !== "number")
throw new Error("The given object does not match the interface `temp`");
return true;
}
您可以通过使用实验装饰器来自动执行其中的一些操作。见http://blog.wolksoftware.com/decorators-metadata-reflection-in-typescript-from-novice-to-expert-part-4