typescript - 了解 Typescript 定义

标签 typescript d3.js

来自@types/d3定义,我们有以下几行:

export type ValueFn<T extends BaseType, Datum, Result> = (this: T, datum: Datum, index: number, groups: T[] | ArrayLike<T>) => Result;

attrTween(name: string): ValueFn<GElement, Datum, (t: number) => string> | undefined;

当我使用函数attrTween时,我这样使用它:

caller.attrTween("points", (d, index, groups) => { return (t) => {return "";};  }

其中调用者是 Transition<BaseType, PieArcDatum<number>

VSCode 将这些参数推断为:

(parameter) d: d3.PieArcDatum<number>
(parameter) index: number
(parameter) groups: d3.BaseType[] | d3.ArrayLike<d3.BaseType>

类型定义如何强制执行此行为? 能否逐字解释,特别是通用用法?

最佳答案

attrTweenthree separate overloads ,其中第三个条目(并且只有第二个参数非空的双参数重载)是:

/**
 * Assign the attribute tween for the attribute with the specified name to the specified interpolator factory.
 * An interpolator factory is a function that returns an interpolator; when the transition starts, the factory is evaluated for each selected element.
 * The returned interpolator will then be invoked for each frame of the transition, in order,
 * being passed the eased time t, typically in the range [0, 1]. Lastly, the return value of the interpolator will be used to set the attribute value.
 * The interpolator must return a string.
 *
 * @param name Name of attribute.
 * @param factory An interpolator factory which is evaluated for each selected element, in order, being passed the current datum (d),
 * the current index (i), and the current group (nodes), with this as the current DOM element (nodes[i]). The interpolator factory returns a string interpolator,
 * which takes as its argument eased time t, typically in the range [0, 1] and returns the interpolated string.
 */
attrTween(name: string, factory: ValueFn<GElement, Datum, (t: number) => string>): this;

这比您列出的重载更符合您的调用。这个定义存在于Transition中,其定义如下:

export interface Transition<GElement extends BaseType, Datum, PElement extends BaseType, PDatum>

这样,我们就可以看到直接映射了:

  • dDatum ,来自callerd3.PieArcDatum<number>
  • indexnumber
  • groupsT[] | ArrayLike<T>哪里T extends BaseType
    • attrTween的定义,TValueFnGElement 相同在Transition .
    • GElementBaseType根据您的callee .
    • 因此TBaseType ,所以groupsd3.BaseType[] | d3.ArrayLike<d3.BaseType>如您所见。

关于typescript - 了解 Typescript 定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56377760/

相关文章:

typescript - 使用 Aurelia 的 Fetch Client 时 Azure Translator API(认知服务)上出现 CORS 错误

javascript - Cucumber javascript - 如何自动生成步骤定义文件?

typescript - 创建并返回可分配给对象的特定键的值

javascript - d3 图表的工具提示无法正确解析 2009 年 2 月

javascript - 缩放 SVG 但仅此而已?

css - 在 Next.js 中应用全局样式

javascript - 使 DOM 元素的高度相等

d3.js 堆栈布局从 v3 升级到 v4

javascript - D3 js图表倒置问题

javascript - d3.js 范围选择器不过滤工具提示