简体   繁体   English

TypeScript:获取Class的非继承属性

[英]TypeScript: Get Non-Inherited Properties of Class

class A {
    baseField: number;
}

class B extends A {
    nonInherited: number;
}

type onlyNonInherited = PickNonInherited<B>;

I need my onlyNonInherited type to be equal to the following:我需要我的onlyNonInherited类型等于以下内容:

interface NonInherited {
    nonInherited: number;
}

What should be the definition of PickNonInherited ? PickNonInherited的定义应该是什么? I've tried to read many posts, but found no solution:(我试图阅读很多帖子,但没有找到解决方案:(

It's not possible to do this by only looking at the type B .仅查看类型B是不可能做到这一点的。 You can however do something like但是你可以做类似的事情

type OnlyNonInherited = Omit<B, keyof A>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM