react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

Some Inputs with choices in their props do not accept `ReadonlyArray<T>`.

Open na2na-p opened this issue 2 years ago • 1 comments

What you were expecting:

SelectInput choices should accept ReadonlyArray<T>

What happened instead:

SelectInput choices does not accept ReadonlyArray<T>

image

Steps to reproduce:

  • use "react-admin": "^4.12.3"

Related code:

  • Preferably, a sandbox forked from
  • L129
  • L151

https://stackblitz.com/edit/github-h13gjc?file=src%2Fposts%2FPostEdit.tsx

  • A link to a GitHub repo with the minimal codebase to reproduce the issue
import { SelectInput } from 'react-admin';

type Choice = {
  id: number;
  name: string;
};

const mutableArray = [
  {
    id: 1,
    name: 'foo',
  },
  {
    id: 2,
    name: 'bar',
  },
  {
    id: 3,
    name: 'baz',
  },
] satisfies Array<Choice>;

const readonlyArray = [
  {
    id: 1,
    name: 'foo',
  },
  {
    id: 2,
    name: 'bar',
  },
  {
    id: 3,
    name: 'baz',
  },
] as const satisfies ReadonlyArray<Choice>;

export const SelectInputGroup = () => {
  return (
    <>
      <SelectInput source="mutableChoices" choices={mutableArray} />
      <SelectInput source="immutableChoices" choices={readonlyArray} /> // type error
    </>
  );
};

Other information:

My suggestion (though more than just the SelectInput needs to be inspected):

-     choices?: any[];
+     choices?: readonly any[]; // or ReadonlyArray<any>

https://github.com/marmelab/react-admin/blob/de8fbaa3285714aab914e0e76cc369c09a817d3c/packages/ra-core/src/form/useChoices.tsx#L16

This is a small example. https://www.typescriptlang.org/play?#code/MYewdgzgLgBAtgVygQwEYBsCmBBATr5ATwC4Y8DCAeaXASzAHMA+GAXhgG0ByAMxBC4AaGF1TJcQkWIBeXALoBuAFChIsXJmQATcOkLkipAEqadYPQao16zNp179Jo8U5nzlSngjDAotcDCYAB7IcAAOWABi3sAAjAAUAPqIKBg4uAwQpJaUyGCETACUMADeSjAVMBpQCLhgygC+Hl4+fgHBoRGY0T4ATEm0cCloWHiZxqa6+vhEuflFpeWV1bX1Sk1KSh3hUTEJw2mWhcrbXT1x8RraU0cep7t98QejM4THWyE73TH9V2YWr3eQA

Environment

  • React-admin version: 4.12.3
  • Last version that did not exhibit the issue (if applicable):
  • React version: 18.2.0
  • Browser: Google Chrome 115.0.5790.114 (Official Build) (arm64)
  • Stack trace (in case of a JS error):

na2na-p avatar Aug 02 '23 07:08 na2na-p

Thanks for the report.

This is a low-priority as readonly types aren't really usable at the time IMHO (see https://github.com/Microsoft/TypeScript/issues/13347). I'll mark it as a bug but we won't wok on it in the near future.

fzaninotto avatar Aug 03 '23 13:08 fzaninotto