typescript 合并两个接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49723173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Merge Two Interfaces
提问by Estus Flask
Seeking confirmation or clarification
寻求确认或澄清
If I have two interfaces. What is the "proper" way to create a merge of those two interfaces.
如果我有两个接口。创建这两个接口的合并的“正确”方法是什么。
Foo
富
IFoo{
// some stuff
}
Bar
酒吧
IBar{
// some stuff
}
IFooBar
IFooBar
IFooBar extends IFoo, IBar{
// Empty
}
It works but it feels weird, like I am doing it wrong with the empty IFooBar.
它有效,但感觉很奇怪,就像我对空的 IFooBar 做错了一样。
Am I doing this correctly?
我这样做正确吗?
I also noticed that this also works:
我还注意到这也有效:
type IFooBar = IFoo & IBar;
I have an illogical aversion to using type
yet, it is much cleaner.
我对使用有一种不合逻辑的厌恶type
,它更干净。
回答by Estus Flask
This articleexplains the relation between interfaces and type aliases very well, this partis focused on small differences between them.
这篇文章很好地解释了接口和类型别名之间的关系,这部分主要关注它们之间的细微差别。
Both
两个都
interface IFooBar extends IFoo, IBar {}
and
和
type IFooBar = IFoo & IBar;
are common ways to do this and will behave identically in most cases. Since type
takes less characters to type, it could be chosen for that reason.
是执行此操作的常用方法,并且在大多数情况下表现相同。由于type
需要较少的字符来键入,因此可以选择它。
The inconsistency that is caused by mixed interface
and type
shouldn't be a problem; they are just suitable features to achieve the goal. If const BarClass = FooClass
does the job, class BarClass extends FooClass {}
shouldn't be preferred just because its consistently uses class
everywhere (this example is used for illustrative purposes, there's a considerable difference between these approaches).
由混合引起的不一致性interface
和type
不应该是一个问题; 它们只是实现目标的合适特征。如果可以const BarClass = FooClass
完成这项工作,则class BarClass extends FooClass {}
不应仅仅因为它始终class
在任何地方使用而成为首选(此示例用于说明目的,这些方法之间存在相当大的差异)。
Even though interface
and type
can behave similarly, there is a difference in case of merged interface (also covered in linked article). This will work:
尽管interface
和type
行为相似,但合并界面的情况有所不同(也在链接文章中介绍)。这将起作用:
interface FooBar extends IFoo, IBar {}
class FooBar { ... }
And this will cause type error:
这将导致类型错误:
type FooBar = IFoo & IBar;
class FooBar { ... }
回答by Stephen Paul
If you're wanting to merge 2 interfaces which contain members more than 1 level deep:
如果您想合并包含深度超过 1 级的成员的 2 个接口:
export interface TypeOne {
one: {
two: {
hello: string;
}[]
}
}
export type TypeTwo = {
one: {
two: {
world: string;
}[]
}
} & TypeOne;
const x: TypeTwo;
x.one.two[0]. // autocompletes options are 'hello' / 'world'
回答by Pavel
I think there it is ok, or not ok relating to what meaning of the merged interface. If IFooBar
is a new entity from perspective of object-oriented design, then empty interface is all right. But if there is no such entity, but you want just merge some unrelated interfaces (for some hacky code) - then just use IFoo & IBar
in variable type definition, or type
for shortening this.
我认为这与合并接口的含义无关。如果IFooBar
是面向对象设计的新实体,那么空接口也可以。但是,如果没有这样的实体,但您只想合并一些不相关的接口(对于某些 hacky 代码) - 那么只需IFoo & IBar
在变量类型定义中使用,或者type
缩短它。
It's just my opinion as programmer, that came from object oriented languages like C++ and C#.
这只是我作为程序员的看法,来自 C++ 和 C# 等面向对象的语言。