来自@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>
类型定义如何强制执行此行为? 能否逐字解释,特别是通用用法?
最佳答案
attrTween
有three 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>
这样,我们就可以看到直接映射了:
-
d
是Datum
,来自caller
是d3.PieArcDatum<number>
-
index
是number
-
groups
是T[] | ArrayLike<T>
哪里T extends BaseType
。- 每
attrTween
的定义,T
在ValueFn
与GElement
相同在Transition
. -
GElement
是BaseType
根据您的callee
. - 因此
T
是BaseType
,所以groups
是d3.BaseType[] | d3.ArrayLike<d3.BaseType>
如您所见。
- 每
关于typescript - 了解 Typescript 定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56377760/