[英]Typescript compiles even though I don't implement interface
I have the following interface: 我有以下界面:
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;
}
And this class: 和这个类:
export class UpdateBusinessAddressRequest implements IUpdateBusinessAddressRequest {
}
I'm not implementing the interface in the class, yet my Typescript compiles anyway. 我没有在类中实现接口,但是我的Typescript仍然可以编译。
I have this method inside another class: 我在另一个类中有此方法:
test(request: IDeleteBusinessRequest) {
//stuff
}
IDeleteBusinessRequest is another interface. IDeleteBusinessRequest是另一个接口。 I can call it like so with no compile errors:
我可以这样称呼它,没有编译错误:
this.test(new UpdateBusinessAddressRequest());
UpdateBusinessAddressRequest doesn't implement IDeleteBusinessRequest , but I don't get any compile errors. UpdateBusinessAddressRequest没有实现IDeleteBusinessRequest ,但是我没有得到任何编译错误。
In C# for example, this wouldn't compile. 例如,在C#中,它将无法编译。 Am I just expecting too much of typescript, or am I doing something wrong?
我只是期望打字稿太多吗,还是做错了什么?
I am using Visual Studio 2015 RC with the latest Typescript compiler. 我正在将Visual Studio 2015 RC与最新的Typescript编译器一起使用。
Thanks 谢谢
The problem is the question mark "?", it means that the property is optional, that's why there is no compiler error. 问题是问号“?”,表示该属性是可选的,这就是为什么没有编译器错误的原因。
I just tried your code in the TS playground and removed "?" 我只是在TS游乐场中尝试了您的代码,然后删除了“?” on BusinessId and get the following error :
在BusinessId上,得到以下错误:
Class 'UpdateBusinessAddressRequest' incorrectly implements interface 'IUpdateBusinessAddressRequest'. 类“ UpdateBusinessAddressRequest”错误地实现了接口“ IUpdateBusinessAddressRequest”。 Property 'BusinessId' is missing in type 'UpdateBusinessAddressRequest' .
类型“ UpdateBusinessAddressRequest”中缺少属性“ BusinessId” 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.