What are the differences between the following?
以下有什么区别?
type Foo = {
foo: string
};
interface Foo {
foo: string;
}
7 个解决方案
#1
67
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.
所以一般来说,如果你只有一个普通的对象类型,如你的问题所示,接口通常是一种更好的方法。如果您发现自己想要编写无法编写为接口的内容,或者只想给出不同名称的内容,则类型别名更好。
#2
5
Also, an interface can be implemented.
此外,可以实现接口。
#3
4
The spec has a good discussion - https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.10
该规范有一个很好的讨论 - https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.10
#4
1
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'
}
#5
1
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.
这里键入Foo和接口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.
接口是契约,以下属性(herefoo:string)应该在对象中。接口不是类。当语言不支持多重继承时使用它。因此,接口可以是不同类之间的通用结构。
class Bar implements Foo {
foo: string;
}
let p: Foo = { foo: 'a string' };
Buttype
and interface
are used in very different context.
Buttype和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.
这里foo的类型是Foo,今天是Date。它就像一个变量decleration,它保存其他变量类型的信息。 type类似于接口,类,函数签名,其他类型甚至值的超集(如类型mood ='Good'|'Bad')。最后类型描述了变量的可能结构或值。
#6
1
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 :)
虽然你可以这样做,但是你不能实现一个类型为Union的类型,这完全合情合理:)
#7
0
Thumb rule
拇指规则
Always use interface
if possible. Otherwise play with type
.
如果可能,始终使用界面。否则玩类型。
See the documentation here. https://www.typescriptlang.org/docs/handbook/advanced-types.html
请参阅此处的文档。 https://www.typescriptlang.org/docs/handbook/advanced-types.html
#1
67
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.
所以一般来说,如果你只有一个普通的对象类型,如你的问题所示,接口通常是一种更好的方法。如果您发现自己想要编写无法编写为接口的内容,或者只想给出不同名称的内容,则类型别名更好。
#2
5
Also, an interface can be implemented.
此外,可以实现接口。
#3
4
The spec has a good discussion - https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.10
该规范有一个很好的讨论 - https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.10
#4
1
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'
}
#5
1
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.
这里键入Foo和接口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.
接口是契约,以下属性(herefoo:string)应该在对象中。接口不是类。当语言不支持多重继承时使用它。因此,接口可以是不同类之间的通用结构。
class Bar implements Foo {
foo: string;
}
let p: Foo = { foo: 'a string' };
Buttype
and interface
are used in very different context.
Buttype和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.
这里foo的类型是Foo,今天是Date。它就像一个变量decleration,它保存其他变量类型的信息。 type类似于接口,类,函数签名,其他类型甚至值的超集(如类型mood ='Good'|'Bad')。最后类型描述了变量的可能结构或值。
#6
1
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 :)
虽然你可以这样做,但是你不能实现一个类型为Union的类型,这完全合情合理:)
#7
0
Thumb rule
拇指规则
Always use interface
if possible. Otherwise play with type
.
如果可能,始终使用界面。否则玩类型。
See the documentation here. https://www.typescriptlang.org/docs/handbook/advanced-types.html
请参阅此处的文档。 https://www.typescriptlang.org/docs/handbook/advanced-types.html