0

I have had quite a few look at the flow documentation, with some question still in mind. I have write a constructor named Track ;with my best attempt to type annotate it still not fully understanding it.

Here is the code:

javascript
//@flow
"use strict";
/**
 *A track is compatible with
 * other track and can be parsed to
 *a higher order function using the
 * `this keyword.
 */
type ITrack<T> = {
  x: T,
  y: T,
  width: T,
};

 /**
 * A Track is an adjustable and join-able imaginary ray
 * That stretches out indefinite with a fixed starting x position.
 *@Function Track
 *@Param x -The x position to place the track
 *@Param y - The y position to palce the track
 *@Param width - The width of the track.
 **/

function Track<ITrack> (x, y, width) {
  this.x = x;
  this.y = y;
  this.width = width;
}
  • Firslty,I would like to justify my intention,I am building a tree widget from the html5 canvas. it must sit on a Track – prometheus Dec 15 '20 at 15:56

1 Answers1

0

To annotate a function in flow, you would actually do this inline. Something like this would probably suit you better

// @flow

function Track(x: number, y: number, width: number): void {
  this.x = x;
  this.y = y;
  this.width = width;
}

What you were trying to do before was create a type that accepted generics, but T could then be a range of types like it could be all booleans and that's not really what you want I imagine.

Brianzchen
  • 629
  • 1
  • 15
  • After you wrote that,i have come up with a solution involving generics: ```javascript function Track(x: number, y: number, width: number): T { this.x = x; this.y = y; this.width = width; return this; } ``` – prometheus Dec 16 '20 at 10:59
  • you can check you types with flow check of the project – prometheus Dec 16 '20 at 12:09