From 07913026995d5460d5ce0e906aa3e91a8e2b7014 Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Sat, 25 Jun 2022 17:00:02 -0400 Subject: [PATCH] Improve types for ArcdePhysics.closest This uses generics so that the return type from closest() is narrowed to match whatever type you passed in for the eligible targets. Resulting typescript diff: ```diff --- a/types/phaser.d.ts +++ b/types/phaser.d.ts @@ -70565,7 +70565,7 @@ declare namespace Phaser { * @param source Any object with public `x` and `y` properties, such as a Game Object or Geometry object. * @param targets The targets. */ - closest(source: any, targets?: Phaser.Physics.Arcade.Body[] | Phaser.Physics.Arcade.StaticBody[] | Phaser.GameObjects.GameObject[]): Phaser.Physics.Arcade.Body | Phaser.Physics.Arcade.StaticBody | Phaser.GameObjects.GameObject; + closest(source: Phaser.Types.Math.Vector2Like, targets?: Target[]): Target | null; /** * Finds the Body or Game Object farthest from a source point or object. (END) ``` --- src/physics/arcade/ArcadePhysics.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/physics/arcade/ArcadePhysics.js b/src/physics/arcade/ArcadePhysics.js index 1edf12845..50453ca71 100644 --- a/src/physics/arcade/ArcadePhysics.js +++ b/src/physics/arcade/ArcadePhysics.js @@ -417,10 +417,11 @@ var ArcadePhysics = new Class({ * @method Phaser.Physics.Arcade.ArcadePhysics#closest * @since 3.0.0 * - * @param {any} source - Any object with public `x` and `y` properties, such as a Game Object or Geometry object. - * @param {(Phaser.Physics.Arcade.Body[]|Phaser.Physics.Arcade.StaticBody[]|Phaser.GameObjects.GameObject[])} [targets] - The targets. + * @generic {Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody|Phaser.GameObjects.GameObject} Target + * @param {Phaser.Types.Math.Vector2Like} source - Any object with public `x` and `y` properties, such as a Game Object or Geometry object. + * @param {Target[]} [targets] - The targets. * - * @return {?(Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody|Phaser.GameObjects.GameObject)} The target closest to the given source point. + * @return {Target|null} The target closest to the given source point. */ closest: function (source, targets) {