[英]No error from Typescript compiler when constructor receives different parameters types as the ones from the implemented interface
我尝试了以下代码:
interface IRectangle {
height: number;
width: number;
getArea: ()=>number;
}
module Shapes {
export class Rectangle implements IRectangle {
constructor(public height, public width) {
}
getArea() {
return this.width * this.height;
}
}
}
console.log(new Shapes.Rectangle(12, 'a').getArea());
我期望在尝试向构造函数提供字符(最后一个代码行)时,即使接口(IRectangle)说两个字段都应为数字类型时,打字稿编译器也会给我一个错误。 但是我没有收到任何错误。 为什么这种行为背后的原因是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.