T
T
timonck2019-12-23 12:36:49
JavaScript
timonck, 2019-12-23 12:36:49

How correctly to register types through Flow?

I have a class for which I need to write types

class Point {
    constructor(x:number, y:number) {
        if (arguments.length > 2 || arguments.length < 2) {
            throw new Error('You send more or less arguments')
        }
        this.x = x;
        this.y = y;
    }

    static randomNumber(from:number, to:number) {
        if (arguments.length > 2 || arguments.length < 2) {
            throw new Error('You send more or less arguments')
        }
        return Math.random() * (to - from) + from;
    }

    static randomPoint(xFrom:number, xTo:number, yFrom:number, yTo:number) {
        if (arguments.length > 4 || arguments.length < 4) {
            throw new Error('You send more or less arguments')
        }
        return new Point(
            Point.randomNumber(xFrom, xTo), Point.randomNumber(yFrom, yTo)
        )
    }
}

throws out Cannot assign `x` to `this.x` because property `x` is missing in `Point`
How to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-12-23
@timonck

class Point {
  x: number;
  y: number;

  // дальше всё по-старому, constructor и т.д.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question