2

例子:

class Test {
  @IsString()
  readonly table!: string;
}
// note, that we call it with a completely wrong type: string instead of JSON-object
const result = plainToClass(Test, 'not a class');

然后result是输入字符串'not a class'

我希望plainToClass

  • Test返回所有属性未定义的类型实例,或
  • 抛出异常

这是一个错误还是我错过了什么?

我还发现无法按预期进行这项工作。

现在的解决方法是显式类型检查并输入我自己的代码:

if (!(result instanceof Test) throw ...;

使用类转换器 0.2.3

4

0 回答 0