phaserjs / phaserjs/phaser

`BaseTween#callbacks` type is inconsistent with runtime behaviour

Open
#7,353 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
40.3k
Forks
7.2k
PR merge metrics
No merged PRs in 30d

Description

Version

  • Phaser Version: N/A (Occurs on master)
  • Operating system: N/A
  • Browser: N/A

Description

The type for callbacks is incorrect - instead of a partial object that might have each callback, it's an object that ALWAYS has every single callback (though each may be set to null).

https://github.com/phaserjs/phaser/blob/41be1e462bc600064e498cba370bfa8c5c055a22/src/tweens/tween/BaseTween.js#L220-L231

MOREOVER, the data added by setCallback is NOT a simple lambda function! It's an object containing the extra parameters!
https://github.com/phaserjs/phaser/blob/41be1e462bc600064e498cba370bfa8c5c055a22/src/tweens/tween/BaseTween.js#L621-L624

This is BLATANTLY type-unsafe due to effectively misrepresenting the type's very nature - the declaration file says "record of lambda functions", the code says "record of objects with lambda functions".

Current (incorrect) typing

type TweenCallbacks = {
  /**
   * A function to call when the tween becomes active within the Tween Manager.
   */
  onActive?: Phaser.Types.Tweens.TweenOnActiveCallback;
  /**
   * A function to call when the tween starts playback, after any delays have expired.
   */
  onStart?: Phaser.Types.Tweens.TweenOnStartCallback;
  /**
   * A function to call when the tween completes.
   */
  onComplete?: Phaser.Types.Tweens.TweenOnCompleteCallback;
  /**
   * A function to call each time the tween loops.
   */
  onLoop?: Phaser.Types.Tweens.TweenOnLoopCallback;
  /**
   * A function to call each time the tween is paused.
   */
  onPause?: Phaser.Types.Tweens.TweenOnPauseCallback;
  /**
   * A function to call each time the tween is resumed.
   */
  onResume?: Phaser.Types.Tweens.TweenOnResumeCallback;
  /**
   * A function to call each time the tween repeats. Called once per property per target.
   */
  onRepeat?: Phaser.Types.Tweens.TweenOnRepeatCallback;
  /**
   * A function to call when the tween is stopped.
   */
  onStop?: Phaser.Types.Tweens.TweenOnStopCallback;
  /**
   * A function to call each time the tween steps. Called once per property per target.
   */
  onUpdate?: Phaser.Types.Tweens.TweenOnUpdateCallback;
  /**
   * A function to call each time the tween yoyos. Called once per property per target.
   */
  onYoyo?: Phaser.Types.Tweens.TweenOnYoyoCallback;
};

Example Test Code

const tween = scene.tweens.add({targets: sprite, duration: 400, on});
if (tween.callbacks.onComplete === null) {} // TS says impossible, but actually runs
tween.callbacks.onComplete?.() // CRASHES because it's not a function!

Additional Information

The callbacks themselves should also probably be readonly, given the setCallback function's entire purpose is to modify them.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked callbacks definition and setCallback implementation in src/tweens/tween/BaseTween.js, then locate the declaration file for TweenCallbacks and the related callback types. Compare the declaration with the runtime callback objects and null values; done means the typings accurately represent both shapes and callback mutation behavior, with the example type checks passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
game-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.