Skip to content

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

Open
@na2na-p

Description

@na2na-p

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>

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):

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions