小编Bas*_*adi的帖子

TS接口不强制实现者使用功能签名

interface test{
    foo(boo:string);
}
class coo implements test{
    foo(){

    }
}
Run Code Online (Sandbox Code Playgroud)

playGround 中,虽然函数签名不像接口所说的那样但这并不会生成和错误,接口的预期行为是强制签名.

为什么会这样?

谢谢

interface signature typescript

8
推荐指数
1
解决办法
834
查看次数

接口和类中的函数重载 - 如何?

我有这个界面:

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: 数字): 任何; }' 不兼容

这样做的正确方法是什么?

谢谢

overloading interface class typescript

6
推荐指数
2
解决办法
1万
查看次数

为什么打字稿允许通过可变类型引用/分配只读类型/属性?

为什么打字稿不强制 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)

readonly typescript

1
推荐指数
1
解决办法
3043
查看次数

标签 统计

typescript ×3

interface ×2

class ×1

overloading ×1

readonly ×1

signature ×1