在 Typescript 中使用接口或类时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51716808/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:56:54  来源:igfitidea点击:

When use a interface or class in Typescript

typescriptclasswebinterfacestrong-typing

提问by Josbel Luna

I have a simple scenario of a login that requires user's input a email and password in Typescript. I need to create some type to get this strong-typed and send it to the back-end.

我有一个简单的登录场景,需要用户在 Typescript 中输入电子邮件和密码。我需要创建一些类型来获取这种强类型并将其发送到后端。

Should this be written as:

这应该写成:

export interface UserLogin {
  email: string;
  password: string;
}
//OR
export class UserLogin {
  email: string;
  password: string;
}

And how to know when is the scenario to use any of these?

以及如何知道何时使用这些场景?

回答by alephnerd

At it's most basic, a classis essentially an object factory(ie. a blueprint of what an object is supposed to look like and then implemented), whereas an interfaceis a structure used solely for type-checking.

在最基本的情况下,本质上是一个对象工厂(即对象应该是什么样子然后实现的蓝图),而接口是一个仅用于类型检查的结构。

While a classmay have initialized properties and methods to help create objects, an interfaceessentially defines the properties and type an object can have.

虽然可能已经初始化了属性和方法来帮助创建对象,但接口本质上定义了对象可以具有的属性和类型。

In the scenario you described, one would use the interfaceto set the type for UserLogin.

在您描述的场景中,您将使用该界面来设置 UserLogin 的类型。

回答by Yakov Fain

If you just need to declare a custom type then use interfaces. IMO, there is a very pragmatic reason - they don't get transpiled into JavaScript so the generated code is shorter.

如果您只需要声明自定义类型,请使用接口。IMO,有一个非常务实的原因 - 它们不会被转换为 JavaScript,因此生成的代码更短。

Interfaces won't work if you need to instantiate objects with constructors or use a framework that instantiates and inject them. Then use classes or abstract classes.

如果您需要使用构造函数实例化对象或使用实例化并注入它们的框架,则接口将不起作用。然后使用类或抽象类。

回答by D. Phi

Actually both will do the job. I would suggest you to use interfaces though if you just do type-checking as it is specifically designed for that purpose.

实际上两者都会完成这项工作。如果您只是进行类型检查,我建议您使用接口,因为它是专门为此目的而设计的。

Classes and interfaces are powerful structures that facilitate not just object-oriented programming but also type-checking in TypeScript. A class is a blueprint from which we can create objects that share the same configuration - properties and methods. An interface is a group of related properties and methods that describe an object, but neither provides implementation nor initialisation for them.

类和接口是强大的结构,不仅有助于面向对象编程,还有助于 TypeScript 中的类型检查。类是一个蓝图,我们可以从中创建共享相同配置(属性和方法)的对象。接口是一组描述对象的相关属性和方法,但既不为它们提供实现也不提供初始化。

From: https://toddmotto.com/classes-vs-interfaces-in-typescript

来自:https: //toddmotto.com/classes-vs-interfaces-in-typescript

回答by artem

send it to the back-end

发送到后端

Sending classes over the wire requires extra effort - if you use JSON content type, your class will come as plain object to your back-end code. See numerous answersfor this question telling how to convert JSON back to a class object so it's possible to call its methods.

通过网络发送类需要额外的努力 - 如果您使用 JSON 内容类型,您的类将作为后端代码的普通对象出现。请参阅此问题的众多答案,了解如何将 JSON 转换回类对象,以便可以调用其方法。

But since the class in your question has no methods, it does not matter, unless the back-end code will perform runtime checks using instanceof.

但是由于您问题中的类没有方法,所以没有关系,除非后端代码将使用instanceof.

To avoid these problems, use interface which has only data members for representing objects for communicating with back-end.

为了避免这些问题,请使用只有数据成员的接口来表示与后端通信的对象。