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

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

问题描述

Typescript 接口和类之间有什么区别?我什么时候用一类?我什么时候使用接口?它们有什么优点?

What is the different between Typescript Interfaces and Classes? When do I use a 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天全站免登陆