Typescript中的接口和类之间的区别 [英] Difference between interfaces and classes in Typescript

查看:486
本文介绍了Typescript中的接口和类之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Typescript接口和类之间的区别是什么?我什么时候使用班级?什么时候使用接口?它们的优点是什么?

what is the different bewtween Typescript Interfaces and Classes? When do I use Class? When do I use Interfaces? What are the advantages of them?

我需要为后端服务器的http请求创建某种类型的内容(使用Angular 2进行此操作),例如: }

I need to create some kind of types for an http-request to my backend server (Doing it with Angular 2), like : },

"fields": {
  "project": {
      "id": "10000"
  },
  "summary": "something's wrong",
  "issuetype": {
      "id": "10000"
  },
  "assignee": {             // not neccesary required
      "name": "homer"
  },
  "reporter": {
      "name": "smithers"
  },
  "priority": {            // not neccesary required
      "id": "20000"
  }
}

我应该使用什么来构建这些模型?谢谢!

What should I use for building these models? Thank you!

推荐答案

2019:差异和用法更新

首先,有一个明显的区别:语法.这是一个简单但了解差异的方法:接口属性可以以逗号或分号结尾,而类属性只能以分号结尾.现在有趣的东西.关于何时使用和不使用的部分可能是主观的-这些是我为团队成员提供的准则,但是出于有效的原因,其他团队也可能会有其他准则.如果您的团队有其他不同的想法,请随时发表评论,我很想知道为什么.

2019: Update on Differences and Usages

First, there is the obvious difference: syntax. This is a simple, but necessary to understand difference: Interface properties can end in commas or semi-colons, however class properties can only end in semi-colons. Now the interesting stuff. The sections about when to use and not to use may be subjective - these are the guidelines I give people on my team, but it is possible that other teams will have other guidelines for valid reasons. Feel free to comment if your team does it differently, I would love to learn why.

接口:允许定义一种类型,该类型将在设计和编译期间用于强类型化.它们可以被实现"或扩展",但是不能被实例化(您不能new它们).在向下编译为JS时它们会被删除,因此它们不占用空间,但是在运行时也无法对其进行类型检查,因此您无法检查变量是否在运行时实现了特定类型(例如foo instanceof bar),除非通过检查它具有的属性:使用Typescript进行接口类型检查.

Interfaces: Allow for defining a type that will be used during design and compile time for strong typing. They can be "implemented" or "extended" but cannot be instantiated (you can't new them). They get removed when transpiling down to JS so they take up no space, but they also cannot be type checked during runtime, so you can't check if a variable implements a specific type at runtime (e.g. foo instanceof bar), except by checking the properties it has: Interface type check with Typescript.

何时使用接口:当您需要为对象创建属性和函数的协定时,请在代码中的多个位置(尤其是多个文件)中使用它们时使用它们或功能.另外,在希望其他对象以该基本属性集开头时使用,例如具有Vehicle接口,多个类将这些接口实现为特定类型的车辆,例如CarTruckBoat(例如class Car implements Vehicle).

When to use interfaces: Use them when you need to create a contract of the properties and functions for an object that will be used in more than one place in your code, especially more than one file or function. Also, use when you want other objects to start with this base set of properties, such as having a Vehicle interface that multiple classes implement as specific types of vehicles, like Car, Truck, Boat (e.g. class Car implements Vehicle).

何时不使用接口:当您希望拥有默认值,实现,构造函数或函数(不仅仅是签名)时.

When not to use interfaces: When you want to have default values, implementations, constructors, or functions (not just signatures).

:还允许定义一种类型,该类型将在设计和编译期间用于强类型化,并且还可以在运行时使用.这也意味着代码没有被编译出来,因此会占用空间.这是@Sakuto提到的一个关键区别,但它的含义不仅仅限于空格.这意味着可以对类进行类型检查,即使在已转译的JS代码中也可以保留对它们是谁"的理解.进一步的差异包括:可以使用new实例化类,并且可以对其进行扩展,但不能实现.类可以具有构造函数和实际函数代码以及默认值.

Classes: Also allow for defining a type that will be used during design and compile time for strong typing, and, additional, can be used during runtime. This also means that the code is not compiled out, so it will take up space. This is one key difference mentioned by @Sakuto, but has more implications than just space. It means that classes can be typed checked, retaining the understanding of "who they are" even in the transpiled JS code. Further differences include: classes can be instantiated using new and can be extended, but not implemented. Classes can have constructors and actual function code along with default values.

何时使用类:当您要创建其中包含实际功能代码的对象时,请使用构造函数进行初始化,并且/或者您要使用new创建它们的实例.同样,对于简单的数据对象,您可以使用类来设置默认值.另一个需要使用它们的时间是在进行类型检查时,但是如果需要,可以使用一些接口的解决方法(请参见接口部分的OS链接).

When to use classes: When you want to create objects that have actual function code in them, have a constructor for initialization, and/or you want to create instances of them with new. Also, for simple data objects, you can use classes for setting up default values. Another time you would want to use them is when you are doing type checking, though there are workarounds for interfaces if needed (see the interface section OS link).

何时不使用类:当您有一个简单的数据接口时,不需要实例化它,当您希望由其他对象实现它时,您只需简单地放置一个接口在现有对象(例如类型定义文件)上,或者在其占用的空间过大或不必要的情况下.附带说明一下,如果您查看.d.ts文件,您会发现它们仅使用接口和类型,因此在转换为TS时将完全删除该文件.

When not to use classes: When you have a simple data interface, do not need to instantiate it, when you want to have it implemented by other objects, when you want to simply put an interface on an existing object (think type definition files) or when the space it would take up is prohibitive or unwarranted. As a side note, if you look in .d.ts files you will notice that they only use interfaces and types, and thus this is completely removed when transpiled to TS.

最后的提示,除了类和接口之外,还有其他两个选项,第一个是称为类型"的东西,它与接口非常相似,但是请查看此SO帖子,特别是2019更新答案: Typescript:接口与类型.最后一种选择是使用TS进行函数式编程(不是OOP).

Final note, there are two other options than just classes and interfaces, the first is something called a "type", which is pretty similar to an interface, but check this SO post, specifically the 2019 Update answer: Typescript: Interfaces vs Types. The last option is to go functional programming style (not OOP) with TS.

有关示例的完整故事,请访问 PassionForDev.com ,有关更多信息,请访问类与继承的示例访问 https://jameshenry.blog/typescript-classes-vs-interfaces /.

For the full story with examples visit PassionForDev.com and more good reading on classes vs. inheritance with examples visit https://jameshenry.blog/typescript-classes-vs-interfaces/.

这篇关于Typescript中的接口和类之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆