简体   繁体   English

即使我没有实现接口,Typescript也会编译

[英]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.

相关问题 Typescript 显示错误,即使它在 VSCode 上编译也是如此 - Typescript shows an error even though it compiles on VSCode 即使我没有实现接口的所有强制属性,Typescript 也没有显示错误 - Typescript did not show error even I did not implement all the compulsory properties of an interface 为什么即使我已经降级了版本,我也无法更改 Typescript 版本 - why I can't change Typescript version even though I have downgraded the version Typescript无法实现通用功能接口 - Typescript Can't implement generic function interface 在打字稿中,如何实现带有回调的接口? - in typescript, how do I implement a interface with a callback? 即使声明了 Typescript 也找不到属性 - Typescript can't find property even though declared 即使安装了定义文件,打字稿也找不到模块 - Typescript can't find module even though definition files are installed 在 TypeScript 中,您如何表示在接口 I 中声明的类型 T 应该是将实现所述接口 I 的任何类型 U - In TypeScript, how do you express that a type T, declared inside an interface I, should be of whatever type U that will implement said interface I 为什么 TypeScript 模块不能实现或遵守接口? 我能解决这个问题吗? - Why can't TypeScript modules implement or adhere to an interface? Can I get around this? TypeScript function 接口实现? - TypeScript Interface implement on function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM