【问题标题】:Typescript compiles even though I don't implement interface即使我没有实现接口,Typescript 也会编译
【发布时间】:2015-06-27 21:20:19
【问题描述】:

我有如下界面:

interface IUpdateBusinessAddressRequest extends IReturn<IBusinessAddressResponse>
{
    BusinessId?: number;
    AddressId?: number;
    AddressLine1?: string;
    AddressLine2?: string;
    AddressLine3?: string;
    TownCity?: string;
    County?: string;
    Postcode?: string;
    Country?: string;
    IsPrimary?: boolean;
}

还有这个类:

export class UpdateBusinessAddressRequest implements IUpdateBusinessAddressRequest  {

}

我没有在类中实现接口,但我的 Typescript 仍然可以编译。

我在另一个类中有这个方法:

test(request: IDeleteBusinessRequest) {
    //stuff
}

IDeleteBusinessRequest 是另一个接口。我可以这样称呼它而没有编译错误:

this.test(new UpdateBusinessAddressRequest());

UpdateBusinessAddressRequest 没有实现 IDeleteBusinessRequest,但我没有收到任何编译错误。

例如,在 C# 中,这不会编译。我只是对打字稿期望过高,还是我做错了什么?

我正在使用带有最新 Typescript 编译器的 Visual Studio 2015 RC。

谢谢

【问题讨论】:

  • 类编译成javascript函数,所以你可以调用它。对我来说看起来很正常。

标签: typescript


【解决方案1】:

问题是问号“?”,表示该属性是可选的,所以没有编译错误。

我刚刚在 TS 操场上尝试了您的代码并删除了“?”在 BusinessId 上并收到以下错误:

“UpdateBusinessAddressRequest”类错误地实现了“IUpdateBusinessAddressRequest”接口。 “UpdateBusinessAddressRequest”类型中缺少属性“BusinessId”

【讨论】:

  • 有道理,谢谢。我已经做 Typescript 大约两天了,所以仍然掌握它!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-24
  • 2011-09-15
  • 2011-02-06
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多