Javascript 在Typescript中,类型和接口有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36782896/
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
In Typescript, what is the difference between type and interface?
提问by martiansnoop
What are the differences between the following?
以下有什么区别?
type Foo = {
foo: string
};
interface Foo {
foo: string;
}
回答by Ryan Cavanaugh
Interfaces can be extended
接口可以扩展
interface A {
x: number;
}
interface B extends A {
y: string;
}
and also augmented
并且还增加了
interface C {
m: boolean;
}
// ... later ...
interface C {
n: number;
}
Type aliases, however, can represent some things interfaces can't
然而,类型别名可以代表一些接口不能代表的东西
type NumOrStr = number | string;
type NeatAndCool = Neat & Cool;
type JustSomeOtherName = SomeType;
So in general if you just have a plain object type, as shown in your question, an interface is usually a better approach. If you find yourself wanting to write something that can't be written as an interface, or want to just give something a different name, a type alias is better.
所以一般来说,如果你只有一个普通的对象类型,如你的问题所示,接口通常是更好的方法。如果你发现自己想写一些不能写成接口的东西,或者只想给一些东西一个不同的名字,类型别名更好。
回答by Dave Ford
Also, an interface can be implemented.
此外,还可以实现接口。
回答by amit77309
Differences between these too are already in this thread.
这些之间的差异也已经在这个线程中。
type Foo = {
foo: string
};
interface Foo {
foo: string;
}
Here type Foo
andinterface Foo
looks almost similar so its confusing.
这里type Foo
和interface Foo
看起来几乎相似,所以它令人困惑。
interface
is contract that the following properties (herefoo:string
) should be there in a object.
interface
is not class
. It is used when language does not support Multiple Inheritance. So interface
can be a common structure between different classes.
interface
约定以下属性(此处foo:string
)应存在于对象中。
interface
不是class
。当语言不支持多重继承时使用它。所以interface
可以是不同类之间的公共结构。
class Bar implements Foo {
foo: string;
}
let p: Foo = { foo: 'a string' };
Buttype
and interface
are used in very different context.
但是type
和interface
在非常不同的上下文中使用。
let foo: Foo;
let today: Date = new Date();
Here type
of foo
is Foo
and today
is Date
.
Its like a variable decleration which holds the information of typeof other variable.
type
is like a superset of interfaces, classes, function signature, other types or even values (like type mood = 'Good' | 'Bad'
).
At the end type
describes the possible structure or value of a variable.
这里type
的foo
isFoo
和today
is Date
。它就像一个变量声明,它保存了其他变量的类型信息。
type
就像接口、类、函数签名、其他类型甚至值(如type mood = 'Good' | 'Bad'
)的超集。最后type
描述了变量的可能结构或值。
回答by nadav
Types is kinda like Interfaces and vice versa: both can implemented by a class. but there are some important differences: 1. when Type is implemented by a class, the properties which belong to the Type must be initialized inside the class, whereas with Interface they must be declared. 2. as @ryan mentioned : Interface can extend another Interface. Types cannot.
类型有点像接口,反之亦然:两者都可以由类实现。但是有一些重要的区别: 1. 当 Type 由类实现时,属于 Type 的属性必须在类内部初始化,而对于 Interface 它们必须声明。2. 正如@ryan 提到的:接口可以扩展另一个接口。类型不能。
type Person = {
name:string;
age:number;
}
// must initialize all props - unlike interface
class Manager implements Person {
name: string = 'John';
age: number = 55;
// can add props and methods
size:string = 'm';
}
const jane : Person = {
name :'Jane',
age:46,
// cannot add more proprs or methods
//size:'s'
}
回答by Dan
It is wrong to say "Interfaces can be implemented" since types can also be implemented
说“接口可以实现”是错误的,因为类型也可以实现
type A = { a: string };
class Test implements A {
a: string;
}
Although you can do this, you can't implement a type that is a Union of types, which makes totally sense honestly :)
虽然你可以做到这一点,但你不能实现一个类型的联合,老实说这是完全合理的:)
回答by udeep shrestha
type in the typescript is used to reference already existing types. It can not be extended like interface
. Examples of type
are:
type 在打字稿中用于引用已经存在的类型。它不能像interface
. 例子type
有:
type Money = number;
type FormElem = React.FormEvent<HTMLFormElement>;
type Person = [string, number, number];
you can use Rest and Spread in types:
您可以在以下类型中使用 Rest 和 Spread:
type Scores = [string, ...number[]];
let ganeshScore = ["Ganesh", 10, 20, 30]
let binodScore = ["Binod", 10, 20, 30, 40]
Interface, on the other hand, allows you to create a NEW TYPE.
另一方面,界面允许您创建新类型。
interface Person{
name: string,
age: number,
}
Interface can be extended with extends keyword.
interface Todo{
text: string;
complete: boolean;
}
type Tags = [string, string, string]
interface TaggedTodo extends Todo{
tags: Tags
}