我试图在 TypeScript 中继承一个类,忽略父类的一些属性。
这是我的代码:
import {IsInt, IsUUID, MaxLength, Min, MinLength, validate} from 'class-validator';
class Person {
@IsUUID()
id: string;
@MinLength(2)
@MaxLength(5)
name: string;
}
class OtherPerson extends Person {
@Min(18)
@IsInt()
age: number;
}
const otherPerson = new OtherPerson();
otherPerson.age = 20;
otherPerson.name = 'kkkk';
validate(otherPerson).then(console.log).catch(console.error);
使用此代码,验证方法返回带有 id 参数的错误。
这是我想要的代码:
import {IsInt, IsUUID, MaxLength, Min, MinLength, validate} from 'class-validator';
class Person {
@IsUUID()
id: string;
@MinLength(2)
@MaxLength(5)
name: string;
}
class OtherPerson extends Omit(Person, ['id']) { // This is that i want
@Min(18)
@IsInt()
age: number;
}
const otherPerson = new OtherPerson();
otherPerson.age = 20;
otherPerson.name = 'kkkk';
validate(otherPerson).then(console.log).catch(console.error);
使用此代码,验证方法不应返回带有 id 参数的错误。
有什么想法可以实现这一目标吗?
谢谢你!
PD:我的想法是基于 NestJS 提供的这些功能,但我无法理解它们是如何工作的:https ://docs.nestjs.com/graphql/mapped-types