interface test{
foo(boo:string);
}
class coo implements test{
foo(){
}
}
Run Code Online (Sandbox Code Playgroud)
在playGround 中,虽然函数签名不像接口所说的那样但这并不会生成和错误,接口的预期行为是强制签名.
为什么会这样?
谢谢
我有这个界面:
interface IPoint {
getDist(): string;
getDist(x: number): any;
}
Run Code Online (Sandbox Code Playgroud)
我需要一个类来实现它,但我无法获得正确的语法来实现该类中的 getDist() 方法..
class Point implements IPoint {
// Constructor
constructor (public x: number, public y: number) { }
pointMethod() { }
getDist() {
Math.sqrt(this.x * this.x + this.y * this.y);
}
// Static member
static origin = new Point(0, 0);
}
Run Code Online (Sandbox Code Playgroud)
它说:
“Point”类声明了接口“IPoint”,但没有实现它:“Point”和“IPoint”类型的属性“getDist”类型不兼容:“() => void”和“{()”类型的调用签名:细绳; (x: 数字): 任何; }' 不兼容
这样做的正确方法是什么?
谢谢
为什么打字稿不强制 readonly 关键字并阻止我们将只读属性传递给非只读属性,这违背了这一点
let foo: {
readonly bar: number;
} = {
bar: 123
};
function iMutateFoo(foo: { bar: number }) {
foo.bar = 456;
}
iMutateFoo(foo); // The foo argument is aliased by the foo parameter
console.log(foo.bar); // 456!```
Run Code Online (Sandbox Code Playgroud)