Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
na2na-p opened this issue Aug 2, 2023 · 1 comment
Open
Labels

Comments

@na2na-p
Copy link

na2na-p commented Aug 2, 2023

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):
@fzaninotto
Copy link
Member

Thanks for the report.

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

@fzaninotto fzaninotto added the bug label Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants