Skip to content

Commit

Permalink
feat: 🎸 AnimationBuilder has more type info, which improves DX
Browse files Browse the repository at this point in the history
AnimationBuilder has two generics now, one of them is for AnimationType
and another one for AnimationOptions that can be used with the specified
AnimationType. It is improves a developer experience of using
AnimationBuilder a lot, since it now suggest completion for your options
e.g. and type checks other information.
  • Loading branch information
ghaiklor committed Apr 20, 2020
1 parent bc7dc2f commit 76e35ce
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"@typescript-eslint/no-type-alias": [
"error",
{
"allowAliases": "in-unions"
"allowAliases": "always",
"allowConditionalTypes": "always"
}
],
"@typescript-eslint/prefer-readonly-parameter-types": [
Expand Down
6 changes: 5 additions & 1 deletion packages/kittik-animation-print/src/Print.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Animation, Animationable } from 'kittik-animation-basic';
import { PrintOptions } from './PrintOptions';
import { Shape } from 'kittik-shape-basic';

export class Print extends Animation implements Animationable {
export { PrintObject } from './PrintObject';
export { PrintOptions } from './PrintOptions';

export class Print extends Animation implements PrintOptions, Animationable {
private originalText = '';

public onTick<S extends Shape, P extends keyof S, V extends number>(shape: S, _property: P, value: V): void {
Expand Down
3 changes: 3 additions & 0 deletions packages/kittik-animation-print/src/PrintObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { AnimationObject } from 'kittik-animation-basic';

export type PrintObject = AnimationObject;
3 changes: 3 additions & 0 deletions packages/kittik-animation-print/src/PrintOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { AnimationOptions } from 'kittik-animation-basic';

export type PrintOptions = AnimationOptions;
31 changes: 14 additions & 17 deletions packages/kittik-slide/src/animation/AnimationBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
import { ANIMATIONS, AnimationType } from './Animations';
import { AnimationObject, AnimationOptions, Animationable, Easing } from 'kittik-animation-basic';
import { ANIMATIONS, AnimationOptions, AnimationType } from './Animations';
import { Animationable } from 'kittik-animation-basic';

export class AnimationBuilder implements AnimationObject {
public type: AnimationType;
public options?: Partial<AnimationOptions>;
export class AnimationBuilder<T extends AnimationType, O extends AnimationOptions<T>> {
public type: T;
public options: Partial<O>;

public constructor (type: AnimationType) {
public constructor (type: T) {
this.type = type;
this.options = {};
}

public static start (type: AnimationType): AnimationBuilder {
public static start <T extends AnimationType, O extends AnimationOptions<T>>(type: T): AnimationBuilder<T, O> {
return new this(type);
}

public withType (type: AnimationType): this {
public withType (type: T): this {
this.type = type;

return this;
}

public withOptions <T extends AnimationOptions>(options: Partial<T>): this {
public withOptions (options: Partial<O>): this {
this.options = { ...this.options, ...options };

return this;
}

public withDuration (duration: number): this {
this.options = { ...this.options, duration };

public withDuration (duration: O['duration']): this {
this.options.duration = duration;
return this;
}

public withEasing (easing: Easing): this {
this.options = { ...this.options, easing };

public withEasing (easing: O['easing']): this {
this.options.easing = easing;
return this;
}

Expand Down
27 changes: 21 additions & 6 deletions packages/kittik-slide/src/animation/Animations.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import { AnimationObject, Animationable } from 'kittik-animation-basic';
import { Focus } from 'kittik-animation-focus';
import { Print } from 'kittik-animation-print';
import { Slide } from 'kittik-animation-slide';
import { Focus, FocusObject, FocusOptions } from 'kittik-animation-focus';
import { Print, PrintObject, PrintOptions } from 'kittik-animation-print';
import { Slide, SlideObject, SlideOptions } from 'kittik-animation-slide';
import { Animation } from 'kittik-animation-basic';

export type AnimationType = 'Focus' | 'Print' | 'Slide';

// eslint-disable-next-line @typescript-eslint/no-extra-parens
export const ANIMATIONS = new Map<AnimationType, { fromObject: <T extends AnimationObject>(obj: T) => Animationable }>([
export type AnimationOptions<T extends AnimationType> = T extends 'Focus'
? FocusOptions
: T extends 'Print'
? PrintOptions
: T extends 'Slide'
? SlideOptions
: never;

export type AnimationObject<T extends AnimationType> = T extends 'Focus'
? FocusObject
: T extends 'Print'
? PrintObject
: T extends 'Slide'
? SlideObject
: never;

export const ANIMATIONS = new Map<AnimationType, typeof Animation>([
['Focus', Focus],
['Print', Print],
['Slide', Slide]
Expand Down

0 comments on commit 76e35ce

Please sign in to comment.