运行时需要 Typescript class 属性
Typescript class property required during runtime
是否可以查明在运行时是否在 Typescript 中声明了 class 属性 是必需的?
export class A {
public readonly ab?: number;
public readonly ac?: number;
public readonly ad: number;
public readonly ae: number;
}
是否可以使用 emitDecoratorMetadata 或 experimentalDecorators 来告知需要 ad 和 ae?
我问是因为我正在写一个 REST API 并且 A 的对象是自动生成的,我想验证在请求时是否实际设置了 ad 和 ae POST数据进来了。
非常感谢
所有类型检查都是关于编译时的,运行 时没有类型检查。如果您想确保接收到的对象有效,您应该手动检查它或使用一个库,它将以某种方式验证服务器响应。
我可以建议 class-validator that supports class validation using decorators as well as plain object validation. To use the class validation, an object should be transformed into the class instance using new Class()
syntax, see Validating plain objects 部分了解更多详情。
是否可以查明在运行时是否在 Typescript 中声明了 class 属性 是必需的?
export class A {
public readonly ab?: number;
public readonly ac?: number;
public readonly ad: number;
public readonly ae: number;
}
是否可以使用 emitDecoratorMetadata 或 experimentalDecorators 来告知需要 ad 和 ae?
我问是因为我正在写一个 REST API 并且 A 的对象是自动生成的,我想验证在请求时是否实际设置了 ad 和 ae POST数据进来了。
非常感谢
所有类型检查都是关于编译时的,运行 时没有类型检查。如果您想确保接收到的对象有效,您应该手动检查它或使用一个库,它将以某种方式验证服务器响应。
我可以建议 class-validator that supports class validation using decorators as well as plain object validation. To use the class validation, an object should be transformed into the class instance using new Class()
syntax, see Validating plain objects 部分了解更多详情。