简体   繁体   English

查询打字稿中的接口

[英]Query about interfaces in typescript

I've started learning TypeScript coming from JavaScript and I'm learning how interfaces work. 我已经开始学习来自JavaScript的TypeScript,并且正在学习接口的工作方式。

I've created the following interface called IEmailable : 我创建了以下名为IEmailable的接口:

interface IEmailable { name: string, email: string }

With the following function called sendEmail passing an object that takes the shape of IEmailable : 通过以下名为sendEmail的函数,可以传递采用IEmailable形状的对象

function sendEmail(contact: IEmailable){
  console.log(contact.name + " <" + contact.email + ">"); }
}

So, running this: 因此,运行此命令:

sendEmail({ 
   name: "Ciaran", 
   email: "ciaran.w@touchcreative.co.uk       
});

Is valid. 已验证。

Whereas running this: 而运行此:

sendEmail({ 
   name: "Ciaran", 
   email: "ciaran.w@touchcreative.co.uk,
   phone: 07927382
});

Is NOT valid. 无效。

In my IDE, adding a new property to an instantiated object that does not exist in the interface throws an error. 在我的IDE中,向接口中不存在的实例化对象添加新属性会引发错误。

"Object literal may only specify known properties" “对象文字只能指定已知属性”

So I understand that adding a property not defined in the interface cannot be done. 因此,我知道无法添加接口中未定义的属性。 However, in the tutorial I follow - it states that adding new properties CAN be added and in their IDE no error was thrown. 但是,在本教程中,我将继续学习-它指出可以添加新属性,并且在其IDE中不会引发任何错误。 Was this a recent update to TypeScript or am I misunderstanding how interfaces work in TypeScript. 这是TypeScript的最新更新吗?还是我误解了TypeScript中接口的工作方式。 I've searched the official docs and in in their example they actually do add a new property size which is doesn't exist in interface LabelledValue . 我已经搜索了官方文档,并且在他们的示例中,他们确实添加了一个新的属性大小 ,该大小在接口LabelledValue中不存在。 I'm definitely overthinking it but if anyone could clear this up that'd be great. 我绝对想得太多,但是如果有人能解决这个问题,那就太好了。

AS per the documentation 根据文档

Object literals get special treatment and undergo excess property checking when assigning them to other variables, or passing them as arguments. If an object literal has any properties that the “target type” doesn't have, you'll get an error.

hence the below code will cause an error as phone does not exists on the interface IEmailable 因此以下代码将导致错误,因为IEmailable接口上不存在phone

sendEmail({ 
   name: "Ciaran", 
   email: "ciaran.w@touchcreative.co.uk,
   phone: 07927382
});

But you can bypass that by using a type assertion like below 但是您可以使用如下类型的断言来绕过它

  sendEmail({ 
   name: "Ciaran", 
   email: "ciaran.w@touchcreative.co.uk",
   phone: 7927382
} as IEmailable);

else you can add a string index signature to the interface to make sure that the object can have some extra property which you can pass as arguments like below 否则,您可以向接口添加字符串索引签名,以确保该对象可以具有一些额外的属性,可以将其作为参数传递,如下所示

interface IEmailable { 
    name: string, 
    email: string,
    [propName: string]: any; 
 }

Here is a working link to the fiddle. 这是到小提琴的有效链接。

"Fiddle" “小提琴”

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM