如果已经准备好了所有类,为什么要在angular中将接口用作数据类型? [英] Why should I use interfaces as data types in angular if there are classes allready?

查看:92
本文介绍了如果已经准备好了所有类,为什么要在angular中将接口用作数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近看了我老师的教程,他向我们展示了这段代码:

I recently watched tutorial of my teacher, and he show us this code:

他还说了 products 数组,它是 any [] 类型的数组.它没有使用TypeScript的强大功能,因此它应该定义为INTERFACE,但是我现在想知道,为什么他没有创建一个类,并且product数组可能是该类类型的?

And he also said products array which is type of any [] IS NOT USING BENEFITS OF STRONGLY TYPE which is feature of TypeScript so it should be defined as INTERFACE, but I'm wondering now, why he did not create a class and that products array might be of that class type??

他说,Interfaces的目的是提供强大的打字和工具支持.所以他写了这个界面:

He said Interfaces purpose is to provide strongly typing and tooling support.. So he wrote this interface:

所以我想知道为什么他没有创建像这样的类:

So I'm wondering why he did not create a class like :

导出类Product ..,后来他将使用 products:Product [] ..

export class Product .. and later he would use products : Product [] ..

所以基本上我不知道为什么在这里将接口作为dataType而不是类更好?

So basically I can not figure out why is Interface as dataType better here instead of class?

对不起截图而不是真实代码,这是因为他主持了在线视频课程.

Sorry for screenshots and not real code, that's because he hosted online video lessons..

谢谢

推荐答案

选中此文字已写入詹姆斯·亨利(James Henry)简而言之:与类不同,接口在编译期间会被完全删除,因此它们不会在我们的最终JavaScript代码中添加任何不必要的内容.

这篇关于如果已经准备好了所有类,为什么要在angular中将接口用作数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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