Skip to content

[@types/lodash] Make sure readonly arrays can't be modified #72536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

DonaldDuck313
Copy link
Contributor

@DonaldDuck313 DonaldDuck313 commented Apr 17, 2025

These methods mutate the array they take as a parameter. Since ReadonlyArray can be converted to List, the old version would allow mutating readonly arrays. With this change, these methods can only take Array parameters which doesn't accept ReadonlyArray.

Please fill in this template.

Select one of these and delete the others:

If adding a new definition: Not applicable

If changing an existing definition:

  • Provide a URL to documentation or source code which provides context for the suggested changes: https://lodash.com/docs/4.17.15
  • If this PR brings the type definitions up to date with a new version of the JS library, update the version number in the package.json. (Not applicable, this was a bug in DefinitelyTyped)

If removing a declaration: Not applicable

These methods mutate the array they take as a parameter. Since ReadonlyArray can be converted to List, the old version would allow mutating readonly arrays. With this change, these methods can only take Array parameters which doesn't accept ReadonlyArray.
@typescript-bot
Copy link
Contributor

typescript-bot commented Apr 17, 2025

@DonaldDuck313 Thank you for submitting this PR!

This is a live comment that I will keep updated.

1 package in this PR

Code Reviews

Because this is a widely-used package, a DT maintainer will need to review it before it can be merged.

You can test the changes of this PR in the Playground.

Status

  • ✅ No merge conflicts
  • ❌ Continuous integration tests have failed
  • 🕐 Most recent commit is approved by a DT maintainer

Once every item on this list is checked, I'll ask you for permission to merge and publish the changes.


Diagnostic Information: What the bot saw about this PR
{
  "type": "info",
  "now": "-",
  "pr_number": 72536,
  "author": "DonaldDuck313",
  "headCommitOid": "faba671aa880ec7f9da19c4c71a7bb0bfdad7ad5",
  "mergeBaseOid": "5d29b9be383902b0399f26072b4590ac61ca72c5",
  "lastPushDate": "2025-04-17T16:59:14.000Z",
  "lastActivityDate": "2025-05-26T11:29:41.000Z",
  "hasMergeConflict": false,
  "isFirstContribution": false,
  "tooManyFiles": false,
  "hugeChange": false,
  "popularityLevel": "Critical",
  "pkgInfo": [
    {
      "name": "lodash",
      "kind": "edit",
      "files": [
        {
          "path": "types/lodash/common/array.d.ts",
          "kind": "definition"
        },
        {
          "path": "types/lodash/lodash-tests.ts",
          "kind": "test"
        }
      ],
      "owners": [
        "bczengel",
        "chrootsu",
        "aj-r",
        "e-cloud",
        "jtmthf",
        "DomiR",
        "WilliamChelman"
      ],
      "addedOwners": [],
      "deletedOwners": [],
      "popularityLevel": "Critical"
    }
  ],
  "reviews": [
    {
      "type": "stale",
      "reviewer": "aj-r",
      "date": "2025-05-10T11:03:35.000Z",
      "abbrOid": "c297f3f"
    }
  ],
  "mainBotCommentID": 2813559509,
  "ciResult": "fail",
  "ciUrl": "https://github.com/DefinitelyTyped/DefinitelyTyped/commit/faba671aa880ec7f9da19c4c71a7bb0bfdad7ad5/checks?check_suite_id=39074608535"
}

@typescript-bot
Copy link
Contributor

🔔 @bczengel @chrootsu @aj-r @e-cloud @jtmthf @DomiR @WilliamChelman — please review this PR in the next few days. Be sure to explicitly select Approve or Request Changes in the GitHub UI so I know what's going on.

@typescript-bot typescript-bot added the The CI failed When GH Actions fails label Apr 17, 2025
@typescript-bot typescript-bot moved this from Waiting for Code Reviews to Needs Author Action in Pull Request Status Board Apr 17, 2025
@typescript-bot
Copy link
Contributor

@DonaldDuck313 The CI build failed! Please review the logs for more information.

Once you've pushed the fixes, the build will automatically re-run. Thanks!

Note: builds that are failing do not end up on the list of PRs for the DT maintainers to review.

@typescript-bot typescript-bot removed the The CI failed When GH Actions fails label Apr 17, 2025
@typescript-bot typescript-bot moved this from Needs Author Action to Waiting for Code Reviews in Pull Request Status Board Apr 17, 2025
@typescript-bot typescript-bot moved this from Waiting for Code Reviews to Needs Maintainer Review in Pull Request Status Board Apr 17, 2025
Copy link
Contributor

@aj-r aj-r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Please also update fp.d.ts (in the root lodash folder). You can do it by hand, the fp-generator doesn't work anymore.

/**
* @see _.fill
*/
fill<T>(array: List<any> | null | undefined, value: T): List<T>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is tricky because while we shouldn't support readonly arrays, we should support mutable array-like objects that can be indexed by number. Maybe we need a new MutableList interface, e.g.

interface MutableList<T> {
    readonly length: number;
    [n: number]: T;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That suggestion doesn't work, and it still doesn't work if I use length instead of readonly length. After some googling I found that there isn't any way to do this, so I guess it's up to you to decide whether you think it's more important to support array-like objects or to not support readonly arrays. I personally think it's more important to not support readonly arrays because I've never actually seen a use case for mutable array-like objects.

Copy link
Contributor

@aj-r aj-r Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather err on the side of not breaking existing code, i.e. keep supporting array-like objects. While rare, somebody is probably using them (and there are some native array-like types such as NodeList and TypedArray that are very likely in use).

So let's either add this overload back, or use the MutableList approach in hopes that Microsoft will eventually fix microsoft/TypeScript#13347, or at least allow people to use linters to catch the error.

Alternatively, you could try adding an extra overload above this one to "trap" readonly arrays and return never, e.g.

fill(array: readonly any[] | null | undefined, value: any): never;

@typescript-bot typescript-bot added the Revision needed This PR needs code changes before it can be merged. label Apr 20, 2025
@typescript-bot typescript-bot moved this from Needs Maintainer Review to Needs Author Action in Pull Request Status Board Apr 20, 2025
@typescript-bot
Copy link
Contributor

@DonaldDuck313 One or more reviewers has requested changes. Please address their comments. I'll be back once they sign off or you've pushed new commits. Thank you!

@typescript-bot typescript-bot removed the Revision needed This PR needs code changes before it can be merged. label Apr 22, 2025
@typescript-bot typescript-bot moved this from Needs Author Action to Waiting for Code Reviews in Pull Request Status Board Apr 22, 2025
@typescript-bot typescript-bot added the The CI failed When GH Actions fails label Apr 22, 2025
@typescript-bot typescript-bot moved this from Waiting for Code Reviews to Needs Author Action in Pull Request Status Board Apr 22, 2025
@typescript-bot
Copy link
Contributor

@DonaldDuck313 The CI build failed! Please review the logs for more information.

Once you've pushed the fixes, the build will automatically re-run. Thanks!

Note: builds that are failing do not end up on the list of PRs for the DT maintainers to review.

@DonaldDuck313
Copy link
Contributor Author

Thanks! Please also update fp.d.ts (in the root lodash folder). You can do it by hand, the fp-generator doesn't work anymore.

I tried that but it seems to have broken the tests. I don't quite understand what fp.d.ts does, any idea what's wrong?

Copy link
Contributor

@aj-r aj-r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that but it seems to have broken the tests. I don't quite understand what fp.d.ts does, any idea what's wrong?

FP is an alternative "functional programming" interface for lodash: https://github.com/lodash/lodash/wiki/fp-guide. It tends to curry and reverse the order of arguments to support a more functional-style paradigm.

However, I just noticed that fp.pull seems to be creating a copy of the input instead of mutating it (since functional programming generally doesn't allow mutation). Sorry for the confusion, you can probably revert the FP change.

/**
* @see _.fill
*/
fill<T>(array: List<any> | null | undefined, value: T): List<T>;
Copy link
Contributor

@aj-r aj-r Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather err on the side of not breaking existing code, i.e. keep supporting array-like objects. While rare, somebody is probably using them (and there are some native array-like types such as NodeList and TypedArray that are very likely in use).

So let's either add this overload back, or use the MutableList approach in hopes that Microsoft will eventually fix microsoft/TypeScript#13347, or at least allow people to use linters to catch the error.

Alternatively, you could try adding an extra overload above this one to "trap" readonly arrays and return never, e.g.

fill(array: readonly any[] | null | undefined, value: any): never;

@typescript-bot typescript-bot added the Revision needed This PR needs code changes before it can be merged. label Apr 24, 2025
@typescript-bot
Copy link
Contributor

@DonaldDuck313 One or more reviewers has requested changes. Please address their comments. I'll be back once they sign off or you've pushed new commits. Thank you!

@typescript-bot typescript-bot removed the Revision needed This PR needs code changes before it can be merged. label Apr 24, 2025
@typescript-bot
Copy link
Contributor

Re-ping @bczengel, @chrootsu, @aj-r, @e-cloud, @jtmthf, @DomiR, @WilliamChelman:

This PR has been out for over a week, yet I haven't seen any reviews.

Could someone please give it some attention? Thanks!

@typescript-bot
Copy link
Contributor

It has been more than two weeks and this PR still has no reviews.

I'll bump it to the DT maintainer queue. Thank you for your patience, @DonaldDuck313.

(Ping @bczengel, @chrootsu, @aj-r, @e-cloud, @jtmthf, @DomiR, @WilliamChelman.)

@typescript-bot typescript-bot moved this from Needs Maintainer Review to Needs Maintainer Action in Pull Request Status Board May 5, 2025
@RyanCavanaugh RyanCavanaugh moved this from Needs Maintainer Action to Waiting for Code Reviews (Blessed) in Pull Request Status Board May 5, 2025
@typescript-bot typescript-bot moved this from Waiting for Code Reviews (Blessed) to Needs Maintainer Action in Pull Request Status Board May 6, 2025
Copy link
Contributor

@aj-r aj-r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review. Nice solution! Can you add tests for this, e.g. use @ts-expect-error to verify that readonly arrays are rejected?

@typescript-bot typescript-bot added the Revision needed This PR needs code changes before it can be merged. label May 10, 2025
@typescript-bot
Copy link
Contributor

@DonaldDuck313 One or more reviewers has requested changes. Please address their comments. I'll be back once they sign off or you've pushed new commits. Thank you!

@typescript-bot typescript-bot removed the Unreviewed No one showed up to review this PR, so it'll be reviewed by a DT maintainer. label May 10, 2025
@typescript-bot typescript-bot moved this from Needs Maintainer Action to Needs Author Action in Pull Request Status Board May 10, 2025
@typescript-bot typescript-bot added Unreviewed No one showed up to review this PR, so it'll be reviewed by a DT maintainer. and removed Revision needed This PR needs code changes before it can be merged. Untested Change This PR does not touch tests labels May 24, 2025
@typescript-bot typescript-bot moved this from Needs Author Action to Needs Maintainer Action in Pull Request Status Board May 24, 2025
@typescript-bot typescript-bot added The CI failed When GH Actions fails and removed Unreviewed No one showed up to review this PR, so it'll be reviewed by a DT maintainer. labels May 24, 2025
@typescript-bot
Copy link
Contributor

@DonaldDuck313 The CI build failed! Please review the logs for more information.

Once you've pushed the fixes, the build will automatically re-run. Thanks!

Note: builds that are failing do not end up on the list of PRs for the DT maintainers to review.

@typescript-bot typescript-bot moved this from Needs Maintainer Action to Needs Author Action in Pull Request Status Board May 24, 2025
@typescript-bot typescript-bot added Unreviewed No one showed up to review this PR, so it'll be reviewed by a DT maintainer. and removed The CI failed When GH Actions fails labels May 24, 2025
@typescript-bot typescript-bot moved this from Needs Author Action to Needs Maintainer Action in Pull Request Status Board May 24, 2025
@typescript-bot typescript-bot added the The CI failed When GH Actions fails label May 24, 2025
@typescript-bot
Copy link
Contributor

@DonaldDuck313 The CI build failed! Please review the logs for more information.

Once you've pushed the fixes, the build will automatically re-run. Thanks!

Note: builds that are failing do not end up on the list of PRs for the DT maintainers to review.

@typescript-bot typescript-bot removed the Unreviewed No one showed up to review this PR, so it'll be reviewed by a DT maintainer. label May 24, 2025
@typescript-bot typescript-bot moved this from Needs Maintainer Action to Needs Author Action in Pull Request Status Board May 24, 2025
@DonaldDuck313
Copy link
Contributor Author

@aj-r I fixed the tests so that they should work, but they're giving strange errors that I don't understand. At line 907 (a blank line) it says "Unused @ts-expect-error". What does "Unused @ts-expect-error" even mean? And why does it give that error at a blank line?

@aj-r
Copy link
Contributor

aj-r commented May 26, 2025

I think it means that the line doesn't contain any error. I'm not sure why it would trigger on a blank line, maybe it's giving you the wrong line number?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Needs Author Action
Development

Successfully merging this pull request may close these issues.

3 participants