[英]typescript class to throw error when undefined is passed as a parameter to constructor
I have a class with a lot of parameters, a simplified version is shown below:我有一个 class 有很多参数,简化版如下所示:
class data {
ID: string;
desp: string;
constructor(con_ID:string,con_desp:string){
this.ID = con_ID;
this.desp = con_desp;
}
}
I am then receiving data from a RESTful call, the body of the call is JSON. It might not have all the parameters requried to create an instance of data
.然后我从 RESTful 调用接收数据,调用的主体是 JSON。它可能没有创建
data
实例所需的所有参数。 Below is an example of the desp
not being passed.下面是
desp
未被传递的示例。
const a = JSON.stringify({ ID: 'bob' });
const b = JSON.parse(a)
If I try to create a new instance of data, it works.如果我尝试创建一个新的数据实例,它就可以工作。
console.log(new data(b['ID'], b['desp']))
>> data { ID: undefined, desp: 'bob' }
How do I reject the construction of the class if a parameter from JSON is undefined
?如果来自 JSON 的参数
undefined
,如何拒绝构建 class?
One method would be to do this for each parameter within the constructor, but I don't think this is the correct solution:一种方法是对构造函数中的每个参数执行此操作,但我认为这不是正确的解决方案:
if (con_ID== undefined){
throw new Error('con_ID is undefined')
}
We can utilize class decorators for this.为此,我们可以使用 class 装饰器。 If we return a class from the decorator then the class' constructor will replace the one defined in code.
如果我们从装饰器返回 class,那么类的构造函数将替换代码中定义的构造函数。 Then we use parameter decorators to store the index of each parameter we wish to check into an array.
然后我们使用参数装饰器将我们希望检查的每个参数的索引存储到一个数组中。
const noUndefinedKey = Symbol("noUndefinedKey");
const NoUndefined: ParameterDecorator = function (target, key, index) {
const data = Reflect.getMetadata(noUndefinedKey, target) ?? [];
data.push(index);
Reflect.defineMetadata(noUndefinedKey, data, target);
};
const Validate = function (target: { new (...args: any[]): any }) {
const data = Reflect.getMetadata(noUndefinedKey, target);
return class extends target {
constructor(...args: any[]) {
data.forEach((index: number) => {
if (typeof args[index] === "undefined") throw new TypeError(`Cannot be undefined.`);
});
super(...args);
}
}
}
Note that reflect-metadata
must be used to use Reflect.getMetadata
and Reflect.defineMetadata
.请注意,必须使用
reflect-metadata
才能使用Reflect.getMetadata
和Reflect.defineMetadata
。 Here's how you would use it:以下是您将如何使用它:
@Validate
class ProtectedFromUndefined {
constructor(@NoUndefined param: string) {}
}
And try a few things:并尝试一些事情:
//@ts-ignore throws error because undefined was provided
new ProtectedFromUndefined()
//@ts-ignore
new ProtectedFromUndefined(undefined)
// is ok
new ProtectedFromUndefined("")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.