【问题标题】:Typing a function with an interface of type in flowjs?在flowjs中使用类型接口键入函数?
【发布时间】:2021-03-26 06:48:17
【问题描述】:

我已经看了很多流程文档,但仍有一些问题。 我编写了一个名为 Track 的构造函数;尽我所能对其进行类型注释 还没有完全理解。

代码如下:

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;
}

【问题讨论】:

  • 首先,我想证明我的意图,我正在从 html5 画布构建一个树小部件。它必须放在轨道上

标签: javascript node.js types flowtype type-systems


【解决方案1】:

要对流中的函数进行注释,您实际上可以内联执行此操作。这样的东西可能更适合你

// @flow

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

您之前尝试做的是创建一个接受泛型的类型,但 T 可能是一系列类型,就像它可能都是布尔值一样,这并不是您想要的我想象的。

【讨论】:

  • 在你写完之后,我想出了一个涉及泛型的解决方案:javascript function Track&lt;T:number&gt;(x: number, y: number, width: number): T { this.x = x; this.y = y; this.width = width; return this; }
  • 您可以通过项目的流程检查 来检查您的类型
猜你喜欢
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 2020-07-31
  • 2014-11-25
  • 1970-01-01
相关资源
最近更新 更多