javascript - 有什么办法可以为自己指定类型吗?或者解决方法 "Types of property are incompatible."

标签 javascript typescript

我有两个类,LineWall。 Line 类是父类,并且具有仅接受 Lines 数组的 collide 函数,并且仅将其过滤回来。
Wall 类执行相同的操作,但针对的是 Wall。

class Line {
    public start: Point
    public end: Point

    constructor(start: Point, end: Point) {
        this.start = start
        this.end = end
    }

    collide(lines: Line[]):Line[] {
        return lines.filter((line) => {return true || false})
    }
}

和墙类:

class Wall extends Line {
    public start: Point
    public end: Point
    public thickness: number

    private collideWith: Wall[]

    constructor(start: Point, end: Point,thickness: number) {
        super(start,end)
        this.thickness = thickness
    }

    collide(walls: Wall[]):Wall[] {
        return this.collideWith = walls.filter((wall) => {return true || false})
        // I would love if this could return just void
        // since it update private property only,
        // but returning Wall[] works too
    }
}

此代码产生错误:

file.ts(line,7): error TS2415: Class 'Wall' incorrectly extends base class 'Line'.
Types of property 'collide' are incompatible.
    Type '(walls: Wall[]) => void' is not assignable to type '(lines: Line[]) => Line[]'.
      Types of parameters 'walls' and 'lines' are incompatible.
        Type 'Wall[]' is not assignable to type 'Line[]'.
          Type 'Wall' is not assignable to type 'Line'.
            Types of property 'collide' are incompatible.
              Type '(walls: Wall[]) => void' is not assignable to type '(lines: Line[]) => Line[]'.
                Types of parameters 'walls' and 'lines' are incompatible.
                  Type 'Wall[]' is not assignable to type 'Line[]'.
                    Type 'Wall' is not assignable to type 'Line'.

遇到这种情况该如何处理?

最佳答案

来自您的代码:

    // I would love if this could return just void
    // since it update private property only,
    // but returning Wall[] works too

要让子类返回 void,父类也必须返回 void。这是因为 OO 多态性。考虑:

var foo:Line;
var bar: Wall;
foo = bar; // polymorphism. After all `Wall` is just `Line` with other stuff
var x = foo.collide(); // Actually calls `bar.collide`. So signatures must be compatible. 

更新(使用self[]类型):

它在 TypeScript 中称为 this 类型。这是一个示例:

interface Point {x:number,y:number}

class Line {
    public start: Point
    public end: Point

    constructor(start: Point, end: Point) {
        this.start = start
        this.end = end
    }

    collide(lines: this[]) {
        return lines.filter((line) => {return true || false});
    }
}

class Wall extends Line {
    public start: Point
    public end: Point
    public thickness: number

    private collideWith: Wall[]

    constructor(start: Point, end: Point,thickness: number) {
        super(start,end)
        this.thickness = thickness
    }

    collide(walls: this[]) {
        return this.collideWith = walls.filter((wall) => {return true || false});
    }
}

关于javascript - 有什么办法可以为自己指定类型吗?或者解决方法 "Types of property are incompatible.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35879735/

相关文章:

javascript - Serial API 中 navigator.serial 和 SerialPort 的功能是否未实现?

Javascript:如何删除 div 内所有输入所需的属性

javascript - 使用 Greasemonkey 将类添加到 iframe 的主体

javascript - 将对象数组转换为具有分组键作为数组的对象的简单方法

typescript - 我可以使用 requirejs 在 typescript 代码中加载模块吗?

javascript - 如何为 AngularJS $q 和 $http 添加 Typescript 类型?

typescript - 如何在角度8中以动态形式仅选择一个复选框

javascript - 防止文档窗口文档脚本

javascript - 使用 Angular.fromJson 数据数组始终处理为字符串而不是数组

javascript - 为什么我收到错误 TypeError : "xyz" is not a function?