React Antd 模态属性“children”在类型“IntrinsicAttributes & ModalProps”上不存在

bbr*_*nck 5 typescript reactjs antd

升级到 React 18 后,我遇到了以下 Antd 问题。

\n

当使用 Antd 中的组件(模态、对话框等)时,出现以下错误:

\n
TS2322: Type '{ children: ReactNode; ref: ForwardedRef<unknown>; className: string; visible: true; onCancel: (() => void) | undefined; onOk: undefined; footer: null; closable: false; }' is not assignable to type 'IntrinsicAttributes & ModalProps'. \xc2\xa0\xc2\xa0Property 'children' does not exist on type 'IntrinsicAttributes & ModalProps'.\n
Run Code Online (Sandbox Code Playgroud)\n

这是我的组件:

\n
function TransitionsModal(props: Props): JSX.Element {\n  const {\n    open,\n    onClose,\n    children\n  } = props;\n\n  return (\n    <div>\n      {open &&\n        <Modal\n          className="flex items-center justify-center"\n          visible={open}\n          onCancel={onClose}\n          onOk={undefined}\n          footer={null}\n          closable={false}>\n          {children}\n        </Modal>\n      }\n    </div>\n\n  );\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这是我的道具:

\n
type Props = {\n  children: React.ReactNode,\n  open: boolean,\n  onClose?: () => void\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我使用节点17.8.0

\n

小智 14

你应该使用

"@types/react": "17.0.39" 
Run Code Online (Sandbox Code Playgroud)

代替

"@types/react": "18.0.3"
Run Code Online (Sandbox Code Playgroud)

因为react18 @types 还不兼容。