微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过忽略属性继承打字稿中的类

如何解决通过忽略属性继承打字稿中的类

我试图在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);

使用此代码,validate方法返回带有id参数的错误

这是我想要的代码

import {IsInt,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参数的错误

有什么想法可以实现?

谢谢!

PD:我的想法基于nestJS提供的这些功能,但我不明白它们是如何发挥作用的:https://docs.nestjs.com/graphql/mapped-types

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。