#Bumping into trouble with ESLint readonly parameters.

8 messages · Page 1 of 1 (latest)

north lark
#

I'm working through a project that involves fixing a lot of linting issues (if possible). One that is PLAGUING me is "Parameter should be a read only type @livid void-eslint/prefer-readonly-parameter-types".

As an example, this is some code for a zustand store (it might be janky, it works what I'm doing I'm really only focused on why this linter issue is happening). And for reference that exact linting error is thrown on lines 11, 15, and 23 (createList and updateList in the interface definition, and when createList is defined on line 23).

The code is attached. If anyone can offer any insight into how to fix the linting issue or why it's happening I would love to know. I would think that in most cases simply using Readonly<> would be enough to alleviate this problem (and functionally leads to cleaner code too?)

Is it in relation to Zustar forcing some kind of mutation of a readonly assignment? Is there any good fix?

short scroll
#

@north lark I tried copying this into the eslint sandbox and the only case I'm seeing it complain about is

Readonly<Omit<TodoListItemType, 'id'>> & { listId: number }

Because that's
Readonly<Type> & MoreStuff
And it wants
Readonly<Type & MoreStuff>

#

Personally, this isn't really an eslint rule I use; though I can see why some might like it.

#

The point is to prevent code like this:

function foo(param: { x: string }) {
  param.x = "foo";
  console.log(param.x)
}

const obj = { x: "bar" };
foo(obj);
obj.x // now "foo" - might be surprising
north lark
#

It's wrapped around an Omit, which needs 2 arguments. I think it's written right from that perspective.

The only reason I care is it's a part of something for a job application and it's the only linter error I can't work my head around. Maybe it has to do with the type imports or something... I really don't know. It seems to be specifically triggering on lines that utilize "TodoListType".

short scroll
#

You still want the & { listId: number } inside the Readonly.

#
Readonly<Omit<TodoListItemType, 'id'> & { listId: number }>
north lark
#

OH my mistake, I misunderstood your comment sorry.