类型' {儿童:元素; }'与类型' intinsicattributes&没有共同的属性。 AnimatePresenceProps' .ts(2559)

发布于 2025-01-20 15:53:57 字数 471 浏览 0 评论 0原文

此错误刚刚开始随机发生:

在此处输入图像描述

但是,如果我制作一个传播道具并引入子项的包装组件,则不会出现错误:

const WorkingVersion = (props: {id?: number}) => <SimpleComponent {...props}><div>hello</div></SimpleComponent>

此问题也不会发生在用于打字稿的codesandbox上,这让我认为这是我的特定问题机器。

我尝试恢复到上一个​​工作版本,但它并没有解决我遇到的问题。

我应该寻找什么可能导致弹出此错误?

This error just started happening randomly:

enter image description here

However if I make a wrapping component that spreads props and introduces children there's no error:

const WorkingVersion = (props: {id?: number}) => <SimpleComponent {...props}><div>hello</div></SimpleComponent>

This issue also does not happen on codesandbox for typescript making me think it's a specific issue on my machine.

I tried reverting to the last working version however it somehow didn't fix the issue I was having.

What should I be looking for that could cause this error to popup?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夜声 2025-01-27 15:53:57

这是最近由 此 PR 在 React v18 类型上引起的错误。其他人也遇到了同样的问题,更新后一切都崩溃了,请参阅此处打开的最近问题。我的建议是降级反应类型,直到他们为此发布修补程序。另请参阅此问题

This is a recent bug caused by this PR on types of react v18. Other people are having same problems where everything breaks down after the update, see the recent issues opened here. My suggestion is to downgrade the react types until they release a hotfix for this. See also this issue

放赐 2025-01-27 15:53:57

今天面临同样的问题。

对我有用的是将 node_modules\@types\react line:511 (功能组件的接口定义)从 更改为

interface FunctionComponent<P = {}> {
    (props: P, context?: any): ReactElement<any, any> | null;
    propTypes?: WeakValidationMap<P> | undefined;
    contextTypes?: ValidationMap<any> | undefined;
    defaultProps?: Partial<P> | undefined;
    displayName?: string | undefined;
}

interface FunctionComponent<P = {}> {
    (props: PropsWithChildren<P>, context?: any): ReactElement<any, any> | null;
    propTypes?: WeakValidationMap<P> | undefined;
    contextTypes?: ValidationMap<any> | undefined;
    defaultProps?: Partial<P> | undefined;
    displayName?: string | undefined;
}

只是一个临时修复,以便能够同时使用框架运动。我们可能必须等到他们发布错误修复 PR。

Faced the same issue today.

What worked for me is changing node_modules\@types\react line:511 which is the interface definition of Function Component from

interface FunctionComponent<P = {}> {
    (props: P, context?: any): ReactElement<any, any> | null;
    propTypes?: WeakValidationMap<P> | undefined;
    contextTypes?: ValidationMap<any> | undefined;
    defaultProps?: Partial<P> | undefined;
    displayName?: string | undefined;
}

to

interface FunctionComponent<P = {}> {
    (props: PropsWithChildren<P>, context?: any): ReactElement<any, any> | null;
    propTypes?: WeakValidationMap<P> | undefined;
    contextTypes?: ValidationMap<any> | undefined;
    defaultProps?: Partial<P> | undefined;
    displayName?: string | undefined;
}

This is just a temporary fix to be able to work with framer-motion in the meantime. We'll probably have to wait till they release the bugfix PR.

思念绕指尖 2025-01-27 15:53:57

这个解决方法为我解决了这个问题。

在项目根目录下创建一个framer-motion.d.ts文件并添加以下代码片段。

import * as React from 'react';

declare module 'framer-motion' {
  export interface AnimatePresenceProps {
    children?: React.ReactNode;
  }
}

This workaround fixed it for me.

Create a framer-motion.d.ts file on the project root directory and add the following code snippet.

import * as React from 'react';

declare module 'framer-motion' {
  export interface AnimatePresenceProps {
    children?: React.ReactNode;
  }
}

我的痛♀有谁懂 2025-01-27 15:53:57

新版本的React 18,社区将儿童视为FC类型的默认道具。使用此代码来解决问题。

Your Code:
  export const ContextClass: React.FC = ({children}) => {}

Solution:
  export const ContextClass = ({children}: {children: React.ReactNode}) => {}

New version of React 18, the Community removed children as a default prop on the FC type. Use this code to fix the issue.

Your Code:
  export const ContextClass: React.FC = ({children}) => {}

Solution:
  export const ContextClass = ({children}: {children: React.ReactNode}) => {}
你在看孤独的风景 2025-01-27 15:53:57

我只是遇到了这个问题,并通过包装我的simperecomponentpropswithChildren&lt; {...}&gt;中解决了它的修复

function SimpleComponent(props: PropsWithChildren<{
  prop1: string;
  prop2: number;
  ...
}>) {
  return <div>...</div>;
}

I just had this issue and fixed it by wrapping the props type declaration of my SimpleComponent in PropsWithChildren<{...}>

Something like this:

function SimpleComponent(props: PropsWithChildren<{
  prop1: string;
  prop2: number;
  ...
}>) {
  return <div>...</div>;
}
美胚控场 2025-01-27 15:53:57

将具有同一名称的组件重新列为例如使用Framer-Motion,然后让VS代码使用该组件。如下:

import {
     AnimatePresenceProps,
     AnimatePresence as _AnimatePresence
} from "framer-motion";

export const AnimatePresence = (
    p: AnimatePresenceProps & {
        children: React.ReactNode;
    }
) => {
    return <_AnimatePresence {...p} />;
};

Redeclare the component with same name for example if using framer-motion, and let VS Code use that component. Like below:

import {
     AnimatePresenceProps,
     AnimatePresence as _AnimatePresence
} from "framer-motion";

export const AnimatePresence = (
    p: AnimatePresenceProps & {
        children: React.ReactNode;
    }
) => {
    return <_AnimatePresence {...p} />;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文