Skip to content
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

Rule proposal: no-unsafe-readonly-mutable-assignment #5954

Closed
6 tasks done
JoshuaKGoldberg opened this issue Nov 9, 2022 · 2 comments
Closed
6 tasks done

Rule proposal: no-unsafe-readonly-mutable-assignment #5954

JoshuaKGoldberg opened this issue Nov 9, 2022 · 2 comments
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@JoshuaKGoldberg
Copy link
Member

JoshuaKGoldberg commented Nov 9, 2022

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
  • My proposal is not a "formatting rule"; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Description

From https://github.com/danielnixon/eslint-plugin-total-functions:

Bans unsafe assignment of readonly values to mutable values (which can lead to surprising mutation in the readonly value). This includes passing readonly values as arguments to functions that expect mutable parameters.

For examples of assignment that this rule considers valid and invalid, see no-unsafe-readonly-mutable-assignment.test.ts.

See microsoft/TypeScript#13347 for a request to fix this at the language level.

Though, I wonder if we can shorten the name a bit?

Fail Cases

type MutableA = { a: string };
type ReadonlyA = { readonly a: string };

const mutate = (mut: MutableA): void => {
    mut.a = "whoops";
};

const readonlyA: ReadonlyA = { a: "readonly?" };
mutate(readonlyA);

Pass Cases

type MutableA = { a: string };
type ReadonlyA = { readonly a: string };

const read = (mut: ReadonlyA): void => {
    // No longer allowed:
    // mut.a = "whoops";
};

const readonlyA: ReadonlyA = { a: "readonly?" };
read(readonlyA);

Additional Info

Forking conversation out from danielnixon/eslint-plugin-total-functions#665.

@JoshuaKGoldberg JoshuaKGoldberg added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look enhancement: new plugin rule New rule request for eslint-plugin labels Nov 9, 2022
@bradzacher bradzacher added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for maintainers to take a look labels Nov 16, 2022
@danielnixon
Copy link
Contributor

This may be a better approach: danielnixon/eslint-plugin-total-functions#708

@bradzacher
Copy link
Member

I believe that this will be fixed by TS 5.5 which is including a new flag --enforceReadonly

microsoft/TypeScript#58296

@bradzacher bradzacher closed this as not planned Won't fix, can't repro, duplicate, stale May 1, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

3 participants