通过省略属性继承typescript中的类
我试图在TypeScript中继承一个类,忽略父类的一些属性 这是我的代码:通过省略属性继承typescript中的类,typescript,inheritance,prototypal-inheritance,reflect-metadata,Typescript,Inheritance,Prototypal Inheritance,Reflect Metadata,我试图在TypeScript中继承一个类,忽略父类的一些属性 这是我的代码: import {IsInt, IsUUID, MaxLength, Min, MinLength, validate} from 'class-validator'; class Person { @IsUUID() id: string; @MinLength(2) @MaxLength(5) name: string; } class OtherPerson exten
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);
使用此代码,validate方法返回id为param的错误
这是我想要的代码:
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);
对于这段代码,validate方法不应该返回id为param的错误
有什么办法可以做到这一点吗
谢谢大家!
PD:我的想法是基于NestJS提供的这些功能,但我不明白它们是如何工作的: