【发布时间】:2019-07-20 15:10:10
【问题描述】:
当你看到标题时,你可能会马上回答“interface A extends B { ... }”,但这不是我想要的。
我的意思是,我有两种类型(例如 A 和 B),我想得到一个类型 C,它具有 A 的所有成员和 B 的成员与 A 的成员具有不同的名称,以及类型A 的成员应该可以分配给 B 的成员。
这一切都与interface A extends B { ... }的行为相同,只是必须通过类型公式获取。
换句话说,我想要这样的东西:
interface C extends B {
[K in keyof A]: A[K];
// this is invalid because members of interfaces must be statically determined in TS
}
// And I don't want an interface.
或者像这样:
// About Omit:
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#the-omit-helper-type
type C = Omit<B, keyof A> & A;
// this can be invalid when B has
// [k: string]: any;
我想知道我是否可以得到如下形式的C型:
type A = { ... }
type B = { ... }
type C = A formula containing A and B
【问题讨论】:
-
如果
B有索引签名,结果应该是什么?索引签名通常不能很好地与映射类型一起使用,但我知道某处有一种解决方法..
标签: typescript