Skip to content

Bug: [prefer-readonly-parameter-types] Date param requires readonly how? #5087

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

Closed
4 tasks done
imcotton opened this issue May 27, 2022 · 2 comments
Closed
4 tasks done
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended

Comments

@imcotton
Copy link

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=4.6.4&sourceType=module&code=PQgEB4CcFMDNpgOwMbVAGwJYCMC8AiaAZwCYAGARgFZ9RgA+AKGcegA8AHAe0gBdRkXREX6IArgFsA+ry5SRkTIgDmoXKAAUiAFyhxE7AgCUa+noB0sgMq9FKjUebtufAUJGgAJgENe0GXISRGqanroAIr7QJrhmnuYAbt7oYtAA8rAOjEA&rules=N4IgAgLgngDgpgZwMYCcCWMIFpEBs0B2EA9DCnAGZwpbkCGAJgPYG5RYx0p0C2cE1LNHgIQALhDUUTFCAC+QA&tsConfig=N4XyA

Repro Code

/// <reference lib="es2015" />



export const num_to_string = (n: number) => n.toString()

export const date_to_ms = (d: Date) => d.valueOf()

ESLint Config

module.exports = {
  "parser": "@typescript-eslint/parser",
  "rules": {
    "@typescript-eslint/prefer-readonly-parameter-types": "error"
  }
}

tsconfig

{
  "compilerOptions": {}
}

Expected Result

No complain with builtin Date type.

Actual Result

Parameter should be a read only type.

Additional Info

No response

Versions

package version
@typescript-eslint/eslint-plugin 5.26.0
@typescript-eslint/parser 5.26.0
TypeScript 4.6.4
ESLint 8.15.0
node web
@imcotton imcotton added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels May 27, 2022
@bradzacher
Copy link
Member

A type is considered readonly if...
... it is an object type whose properties are all marked as readonly, and whose values are all considered readonly.

https://typescript-eslint.io/rules/prefer-readonly-parameter-types#rule-details

You need to make the date type deeply readonly. There are utilities that may be able to assist you in making it deeply readonly.
Or you can look to defining your own immutable date type.
Or you can use a 3rd pparty date lib that has truly immutable date constructs.


Failing that something like #4185 may be desirable.

@bradzacher bradzacher added working as intended Issues that are closed as they are working as intended and removed bug Something isn't working triage Waiting for team members to take a look labels May 27, 2022
@imcotton
Copy link
Author

Thanks for the triage, watching 4185 now, at the meantime found https://www.npmjs.com/package/readonly-date as alternative.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended
Projects
None yet
Development

No branches or pull requests

2 participants