在 TypeScript 中派生仅具有另一个类型的自身属性的最佳方法?
Best way to derive a type with only the own properties of another in TypeScript?
鉴于 interface
s(这些也可能是 class
es):
interface A { a: number }
interface B { b: number }
interface C extends B { c: number }
我如何从 C
获得(导出)一个 type
,它只包含其 自己的属性 ,而不包含继承的属性?
type Cown = ???<C>
我试过的是:
type Cown = Omit<C, keyof B>
这似乎可行,但这是最好的方法吗?
Typescript 类型不会记录它们是如何形成的。 Typescript 不记得它们是如何制作的。这些类型只是它们类型声明的结果。
这意味着 own
属性根本不是类型系统的一部分。
鉴于此,我认为您已经找到了正确的答案。由于一个类型不知道它的部分来自哪里,你只需删除你不想要的那个类型的任何部分。而这段代码正是这样做的:
type Cown = Omit<C, keyof B>
鉴于 interface
s(这些也可能是 class
es):
interface A { a: number }
interface B { b: number }
interface C extends B { c: number }
我如何从 C
获得(导出)一个 type
,它只包含其 自己的属性 ,而不包含继承的属性?
type Cown = ???<C>
我试过的是:
type Cown = Omit<C, keyof B>
这似乎可行,但这是最好的方法吗?
Typescript 类型不会记录它们是如何形成的。 Typescript 不记得它们是如何制作的。这些类型只是它们类型声明的结果。
这意味着 own
属性根本不是类型系统的一部分。
鉴于此,我认为您已经找到了正确的答案。由于一个类型不知道它的部分来自哪里,你只需删除你不想要的那个类型的任何部分。而这段代码正是这样做的:
type Cown = Omit<C, keyof B>