简体   繁体   English

在Typescript中,类型和接口有什么区别?

[英]In Typescript, what is the difference between type and interface?

What are the differences between the following?以下有什么区别?

type Foo = { 
    foo: string 
};
interface Foo {
   foo: string;
}

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.如果你发现自己想写一些不能写成接口的东西,或者只想给一些东西一个不同的名字,类型别名更好。

Differences between these too are already in this thread.这些之间的差异也已经在这个线程中。

type Foo = {
    foo: string
};
interface Foo {
    foo: string;
}

Here type Foo and interface Foo looks almost similar so its confusing.这里的type Foointerface Foo看起来几乎相似,所以很容易混淆。

interface is contract that the following properties (here foo:string ) should be there in a object. interface约定以下属性(此处为foo:string )应存在于对象中。 interface is not class . interface不是class It is used when language does not support Multiple Inheritance.当语言不支持多重继承时使用它。 So interface can be a common structure between different classes.所以interface可以是不同类之间的公共结构。

class Bar implements Foo {
    foo: string;
}

let p: Foo = { foo: 'a string' };

But type and interface are used in very different context.但是typeinterface在非常不同的上下文中使用。

let foo: Foo;
let today: Date = new Date();

Here type of foo is Foo and today is Date .这里的foo typeFootodayDate 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' ). type就像接口、类、函数签名、其他类型甚至值的超集(例如type mood = 'Good' | 'Bad' )。 At the end type describes the possible structure or value of a variable.最后, type描述了变量的可能结构或值。

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 :)虽然你可以做到这一点,但你不能实现一个类型的联合,老实说这是完全合理的:)

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.但是有一些重要的区别: 1. 当Type由类实现时,属于Type的属性必须在类内部初始化,而对于Interface,它们必须声明。 2. as @ryan mentioned : Interface can extend another Interface. 2. 正如@ryan 提到的:接口可以扩展另一个接口。 Types cannot.类型不能。

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'
}

type in the typescript is used to reference already existing types . type 在打字稿中用于引用已经存在的类型 It can not be extended like interface .它不能像interface那样扩展。 Examples of type are: 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
}

此外,还可以实现接口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript中的JQuery和JQueryStatic接口有什么区别? - What is the difference between the JQuery and the JQueryStatic interface in TypeScript? 在 Typescript 中使用 `: Interface` 和 `as Interface` 有什么区别? - What's the difference between using `: Interface` and `as Interface` in Typescript? TypeScript中“声明类”和“接口”有什么区别 - What's the difference between "declare class" and "interface" in TypeScript TypeScript 中的“asserts value is type”和“value is type”有什么区别? - What is the difference between "asserts value is type" and "value is type" in TypeScript? TypeScript声明合并中的导出接口和导出默认接口之间有什么区别? - What's the difference between export interface and export default interface in typescript declaration merging? 使用:和=&gt;对于带有TypeScript函数的返回类型有什么区别? - What is the difference between using : and => for the return type with a TypeScript function? TypeScript 中的这些类型断言或强制转换方法有什么区别 - What are the difference between these type assertion or casting methods in TypeScript 泛型 Type(T) 与 typescript 中的任何类型有什么区别 - What are the difference between generic Type(T) vs any in typescript web api 和接口有什么区别? - What is the difference between web api and interface? Typescript 中对象类型定义的区别 - Difference between object type definitions in Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM