next.js 类型“{ children:Element; }”与类型“IntrinsicAttributes & AnimatePresenceProps”没有共同属性,ts(2559)

wrrgggsh  于 5个月前  发布在  其他
关注(0)|答案(4)|浏览(97)

这个错误开始随机发生:


的数据
然而,如果我做一个 Package 组件来传播 prop 并引入孩子,那就不会有错误:

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

字符串
这个问题也没有发生在codesandbox的typescript使我认为这是一个特定的问题在我的机器上。
我试图恢复到最后的工作版本,但它不知何故没有解决我的问题。
我应该寻找什么可能导致此错误弹出?

oewdyzsn

oewdyzsn1#

这是最近由this PR在react v18的类型上引起的错误。其他人也有同样的问题,在更新后一切都崩溃了,请参阅the recent issues opened here。我的建议是降级react类型,直到他们发布此修复程序。另请参阅this issue

gmxoilav

gmxoilav2#

今天面临同样的问题。
对我有用的是将函数组件的接口定义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;
}


这只是一个暂时的修复,以便在此期间能够与帧运动。我们可能要等到他们发布的错误修复公关。

csga3l58

csga3l583#

这个解决方法帮我解决了这个问题。
在项目根目录下创建一个framer-motion.d.ts文件,并添加以下代码片段。

import * as React from 'react';

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

字符串

wgxvkvu9

wgxvkvu94#

我刚刚遇到了这个问题,并通过将PropsWithChildren<{...}>的props类型声明 Package 在PropsWithChildren<{...}>中来修复它。
就像这样:

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

字符串

相关问题