0
我为Velocityjs做了一个接口,所以我可以在TypeScript中使用它,但我不确定如何正确地为数组类型创建接口。这是拨打电话[]从速度UI包的Velocity.RegisterEffect方法的功能:TypeScript中数组类型的接口
let calls: [{ [key: string]: any }, number, { easing?: string, delay?: number }][] = keyFramesProps.map((p: string): [{ [key: string]: any }, number, { easing?: string, delay?: number }] => {
let anim: KeyFrameSlitted = keyFramesSlitted[p];
let durationPercentage = (+p.replace('%', '')) * 0.01;
return [anim.props, durationPercentage, anim.options];
});
所以我想为类型的接口:[{ [key: string]: any }, number, { easing?: string, delay?: number }]
这是唯一的事情,工作:
interface VelocityCall extends Array<any>{
[0]: { [key: string]: any };
[1]: number;
[2]: { easing?: string, delay?: number };
}
我需要扩展数组,如果不是编译器抱怨数组的缺失方法。
现在我可以这样做:
let calls: VelocityCall[] = keyFramesProps.map((p: string): VelocityCall => {
let anim: KeyFrameSlitted = keyFramesSlitted[p];
let durationPercentage = (+p.replace('%', '')) * 0.01;
return [anim.props, durationPercentage, anim.options];
});
这里是速度界面的其余部分(不VelocityCall)如果有人需要它有一个更好的解决方案:
interface VelocityOptions extends Object {
queue?: string;
duration?: number | "slow" | "normal" | "fast";
easing?: string;
begin?: any;
complete?: any;
progress?: any;
display?: undefined | string;
visibility?: undefined | string;
loop?: boolean;
delay?: number | boolean;
mobileHA?: boolean;
/* Advanced: Set to false to prevent property values from being cached between consecutive Velocity-initiated chain calls. */
_cacheValues?: boolean;
[key: string]: any;
}
interface Velocity {
(element: Element, propertiesMap: "fadeIn" | "fadeOut" | "slideUp" | "slideDown" | "scroll" | "reverse" | "finish" | "finishAll" | "stop" | { [key: string]: any }, options?: VelocityOptions): Promise<Response>;
RegisterEffect(name: string, effect: {
defaultDuration?: number;
calls: [{ [key: string]: any }, number, { easing?: string, delay?: number }][] | [{ [key: string]: any }, number][] | [{ [key: string]: any }][];
reset: { [key: string]: any }
});
}
declare var Velocity: Velocity;
它应该是'[{[键:字符串]:任何},数量,缓解{?:字符串,延缓?: number}]'最后没有额外的'[]',基于你发布的VelocityCall接口 –
对不起,我删除了[]。这是一个VelocityCalls数组(复制过去的错误) – user3717718
当你使用这个表单时,你会得到什么错误?当我这样做时:'let a:[{[key:string]:any},number,{easing?:string,delay?:number}];'我得到变量'a'的所有数组方法。 –