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<Target extends Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody|Phaser.GameObjects.GameObject>(source: Phaser.Types.Math.Vector2Like, targets?: Target[]): Target | null;

                 /**
                  * Finds the Body or Game Object farthest from a source point or object.
(END)
```
This commit is contained in:
Edward Faulkner 2022-06-25 17:00:02 -04:00
parent 54370a0d6c
commit 0791302699

View file

@ -417,10 +417,11 @@ var ArcadePhysics = new Class({
* @method Phaser.Physics.Arcade.ArcadePhysics#closest * @method Phaser.Physics.Arcade.ArcadePhysics#closest
* @since 3.0.0 * @since 3.0.0
* *
* @param {any} source - Any object with public `x` and `y` properties, such as a Game Object or Geometry object. * @generic {Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody|Phaser.GameObjects.GameObject} Target
* @param {(Phaser.Physics.Arcade.Body[]|Phaser.Physics.Arcade.StaticBody[]|Phaser.GameObjects.GameObject[])} [targets] - The targets. * @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) closest: function (source, targets)
{ {