Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

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

Đang mở
#9,155 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@gitstart đang làm issue này rồi.

Từ ngày 3/8/2023.

  • #9162 của @gitstart — đang mở

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
react, typescript
Lĩnh vực
frontend

Hướng nghiên cứu

Bắt đầu với packages/ra-core/src/form/useChoices.tsx tại L16, sau đó tái hiện lỗi TypeScript bằng ví dụ SelectInput và mã StackBlitz được liên kết tại L129 và L151. Kiểm tra các input dựa trên choice khác như đã đề xuất; được xem là hoàn tất khi readonly choices được chấp nhận nhất quán mà không có lỗi TypeScript.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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):
Ngôn ngữ chính
TypeScript
Star
27k
Fork
5.5k
Merge trung bình
1 ngày 3 phút
Pull request đã merge (30 ngày)
32

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của marmelab/react-admin

Tất cả issue của marmelab/react-admin

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.