在TypeScript中使用@devexpress/dx-react-scheduler遇TS错误求助
我在使用@devexpress/dx-react-scheduler库时遇到TypeScript错误,错误信息如下:
Type '{ children: Element[]; data: AppointmentModel[]; }' is not assignable to type 'IntrinsicAttributes & SchedulerProps'. Property 'children' does not exist on type 'IntrinsicAttributes & SchedulerProps'.
我已经参照官方TypeScript指南编写代码,但官方示例仅显示警告,而我的环境中触发的是错误,导致页面无法正常渲染。
解决方法
检查TypeScript严格模式配置
你的环境可能开启了更严格的TypeScript检查(比如tsconfig.json中strict: true或strictPropertyInitialization: true),而官方示例的配置相对宽松。若需保持严格模式,需调整组件用法。修正Scheduler组件的用法
Scheduler组件本身不支持children属性,官方示例的写法属于旧版兼容方式。正确做法是通过plugins属性传入所需插件组件:import { Scheduler } from '@devexpress/dx-react-scheduler'; import { DayView, MonthView } from '@devexpress/dx-react-scheduler-material-ui'; // 错误写法 // <Scheduler data={appointments}> // <DayView /> // <MonthView /> // </Scheduler> // 正确写法 <Scheduler data={appointments} plugins={[<DayView key="day" />, <MonthView key="month" />]} />更新库到最新稳定版
旧版本的类型定义可能存在疏漏,更新@devexpress/dx-react-scheduler及其依赖到最新版本,可修复类型问题:npm update @devexpress/dx-react-scheduler @devexpress/dx-react-core # 若使用Material UI组件,同步更新 npm update @devexpress/dx-react-scheduler-material-ui临时类型断言(不推荐长期使用)
若暂时无法修改结构或更新库,可通过类型断言绕过检查:<Scheduler data={appointments} {...({ children: [<DayView />, <MonthView />] } as any)} />
内容的提问来源于stack exchange,提问作者brislun

