Skip to content

Enforce Mutable Return Types #674

@bitjson

Description

@bitjson

This is a follow up to @RebeccaStevens' #153 which described the issue very well:

Enforce Mutable Return Types

If the return type has to immutable then something very non-functional is going on. What happens to the data after it is returned by a function should be of no concern to the function itself. Mutable return types are more compatible with 3rd-party non-function code as readonly types can not be given as mutable types in TypeScript (while mutable types can be given as either a mutable or readonly type).

An option to enforce this behaviour should be added to the rule (whether this is the default behavior or not is currently undecided).

That issue was mostly solved (and marked as solved) by #480, but as far as I can tell, there isn't yet a way to enforce mutable return types with the new prefer-immutable-types rule.

Maybe a solution could add a "Mutable" option to the available settings for returnTypes.enforcement?

type Options = {
  // ...
  returnTypes?: {
    enforcement: "Mutable" | "None" | "ReadonlyShallow" | "ReadonlyDeep" | "Immutable";
    ignoreInferredTypes: boolean;
    ignoreClasses: boolean | "fieldsOnly";
    ignoreNamePattern?: string[] | string;
    ignoreTypePattern?: string[] | string;
  };
  // ...
};

Am I missing another way in which this feature was added? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: Awaiting FeedbackIssue or PR awaits feedback from the community.Type: IdeaMarks an idea, which might be accepted and implemented.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions