我正在使用angularjs2 + Meteor进行开发。这里的问题是,我已经用以下风格编写了模型-
export interface temp {
name: string;
count: number;
}这里的问题是,我使用typescript进行文件扩展,所以在编译后将其转换为原始文本。所以这个模型是不安全的。用户可以插入任何类型的数据,这是主要的问题。
在meteor + reactJs应用中,我使用了Meteor.collection,它提供了安全性。但是在这里,我们如何让模型在typescript中更安全呢?
发布于 2017-03-31 20:17:47
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
https://stackoverflow.com/questions/43140246
复制相似问题