背景
最近在开发系统过程中,按照antd的官方文档引入DatePicker,但是却报错如下
'DatePicker' cannot be used as a JSX component.
Its instance type 'Component<PickerProps<Moment> & { status?: "" | "error" | "warning" | undefined; }, unknown, any> & CommonPickerMethods' is not a valid JSX element.
The types returned by 'render()' are incompatible between these types.
Type 'React.ReactNode' is not assignable to type 'import("D:/psnp/dmss/dmss_frontend/node_modules/@types/react/index").ReactNode'.
Type '{}' is not assignable to type 'ReactNode'.ts(2786)
过程
- 其实解决办法通过搜索,大概就是更新一下包的版本就可以解决
"@types/react": "^18.0.9",
"@types/react-dom": "^18.0.5",
"typescript": "^4.1.2",
-
具体原因,其实是@types的版本跟typescript的版本冲突了,那我们需要怎么解决呢,其实是可以通过查看来决定安装对应的包版本的。
-
打开npm,查找@types/react
总结
可以看到每个@types/react都有对应的typescript版本,所以大家可以根据现在的目录,去更新对应的包,而不是按照别人的说法,就直接写死版本,也不知道为什么这么解决。