phaserjs / phaserjs/phaser

`Phaser.Math.RND.Pick` and `Phaser.Math.RND.WeightedPick` should use `ArrayLike` interface

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

Nobody has claimed this yet.

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

Description

Version

  • Phaser Version: 3.90.0
  • Operating system: Ubuntu 24.04 LTS (irrelevant)

Description

Currently, Phaser.Math.RandomDataGenerator.Pick and its weighted counterpart require the arguments passed in to be mutable arrays.
Since these functions do not mutate their arguments (and, moreover, do not require anything but numeric index support and a .length property), they should use the more general ArrayLike<T> interface instead.

At the moment, attempting to pass in readonly arrays or array-like objects such as TypedArray will produce type errors despite working perfectly well at runtime.

Image

Example Test Code

import Phaser from "phaser";
const nums = [1, 2, 3] as const;

const randomNum = Phaser.Math.RND.pick(nums);
// type errors since `nums` is readonly, despite running perfectly fine

Additional Information

Trivial to fix; I will create a PR shortly.

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 at the Phaser.Math.RandomDataGenerator.Pick and weighted-pick entry points named in the issue, then inspect their type definitions. Verify that readonly arrays and TypedArray-like inputs are accepted while preserving the existing runtime behavior; use the provided TypeScript example as the completion check.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
developer-experience
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.