fix: behavior type error. closes: #1303.

This commit is contained in:
Yanyan-Wang 2020-03-16 16:58:10 +08:00 committed by Yanyan Wang
parent d04130bbe9
commit a261d98518
2 changed files with 26 additions and 3 deletions

View File

@ -1,6 +1,30 @@
import { clone, each, wrapBehavior } from '@antv/util/lib';
import { BehaviorOption } from '../types';
import { ModelConfig, DefaultBehaviorType } from '../types';
import behaviorOption from './behaviorOption';
import { IGraph } from '../interface/graph';
import { G6Event } from '../types';
type GetEvents = 'getEvents';
type ShouldBegin = 'shouldBegin';
type ShouldUpdate = 'shouldUpdate';
type ShouldEnd = 'shouldEnd';
type Bind = 'bind';
type Unbind = 'unbind';
type BehaviorOption<U> = {
[T in keyof U]: T extends GetEvents
? () => { [key in G6Event]?: string }
: T extends ShouldBegin
? (cfg?: ModelConfig) => boolean
: T extends ShouldEnd
? (cfg?: ModelConfig) => boolean
: T extends ShouldUpdate
? (cfg?: ModelConfig) => boolean
: T extends Bind
? (graph: IGraph) => void
: T extends Unbind
? (graph: IGraph) => void
: (...args: DefaultBehaviorType[]) => unknown;
};
export default class Behavior {
// 所有自定义的 Behavior 的实例
@ -11,7 +35,7 @@ export default class Behavior {
* @param type Behavior
* @param behavior Behavior
*/
public static registerBehavior(type: string, behavior: BehaviorOption) {
public static registerBehavior<T, U>(type: string, behavior: BehaviorOption<U>) {
if (!behavior) {
throw new Error(`please specify handler for this behavior: ${type}`);
}

View File

@ -352,7 +352,6 @@ export interface BehaviorOption {
shouldEnd?(e?: IG6GraphEvent): boolean;
bind?(e: IGraph): void;
unbind?(e: IGraph): void;
[key: string]: (...args) => any;
}
export type IEvent = Record<G6Event, string>;