Skip to content

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

@jendrikw

Description

@jendrikw

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

Since --enforceReadonly was never released (microsoft/TypeScript#58296 is still open), it may be a good idea to think about implementing this.

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

Other implementations:

Previous issue: #5954

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement: new plugin ruleNew rule request for eslint-pluginpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugintriageWaiting for team members to take a look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions