首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型不能赋值给类型'IntrinsicAttributes

类型不能赋值给类型'IntrinsicAttributes
EN

Stack Overflow用户
提问于 2021-09-01 09:34:49
回答 2查看 1.4K关注 0票数 0

我有这个组件

代码语言:javascript
复制
import React, { FunctionComponent, useEffect } from 'react';
import shouldForwardProp from '@styled-system/should-forward-prop';
import styled from 'styled-components';

const delay = 0.25;

interface FadeInSectionProps {
  offset?: number;
  children: React.ReactNode;
}

const Section = styled.div.withConfig({
  shouldForwardProp,
})<{ offset?: number }>`
  transition: 0.5s ease-in-out opacity, 0.5s ease-in-out transform;
  opacity: 0;
  transform: translate(0, -40px);

  &.fade-in-section--is-visible {
    opacity: 1;
    transform: translate(0, 0);
  }

  transition-delay: ${(props) => (props.offset ? props.offset * delay + 's' : '0s')};
`;

export const FadeInSection: FunctionComponent = ({ offset, children }: FadeInSectionProps) => {
  const [isVisible, setVisible] = React.useState(false);
  const domRef = React.useRef();
  useEffect(() => {
    const observer = new IntersectionObserver((entries) => {
      entries.forEach((entry) => {
        if (entry.isIntersecting) {
          setVisible(true);
        }
      });
    });

    observer.observe(domRef.current);
  }, []);
  return (
    <Section
      className={`fade-in-section ${isVisible ? 'fade-in-section--is-visible' : ''}`}
      ref={domRef}
      offset={offset}
    >
      {children}
    </Section>
  );
};

export default FadeInSection;

我正在使用的(在导入之后)如下所示:

代码语言:javascript
复制
<FadeInSection>
   <Header />
<FadeInSection>

代码语言:javascript
复制
<div>
   <FadeInSection>
     <Item1 />
   </FadeInSection>
   <FadeInSection offset={1}>
     <Item1 />
   </FadeInSection>
</div>

但是当我传递属性偏移量时,我得到了这个ts错误(即使它的工作方式与我预期的一样)

英语:

代码语言:javascript
复制
    Type '{ type: string; name: string; value: string; onChange: (e: any) => void; placeholder: string; label: string; }' is not assignable to type 'IntrinsicAttributes & InputProps & { children?: ReactNode; }'.
  Property 'type' does not exist on type 'IntrinsicAttributes & InputProps & { children?: ReactNode; }'.ts(2322)

我哪里做错了?或者我怎样才能摆脱这个错误?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-09-02 10:40:43

如下所示,ide中的警告和lint命令中的错误都消失了。

代码语言:javascript
复制
export const FadeInSection: FunctionComponent<FadeInSectionProps> = ({
  offset,
  children,
}: FadeInSectionProps) => {
   return (<Section />);
}
票数 0
EN

Stack Overflow用户

发布于 2021-09-01 09:43:40

尝试将const FadeInSection: FunctionComponent = ({ offset, children }: FadeInSectionProps)更改为const FadeInSection: FunctionComponent<FadeInSectionProps> = ({ offset, children })

您可以使用FunctionComponent而不是props来键入组件,因此您可以使用像children这样的内部props,而无需键入它们。(您实际上可以从接口中删除子项,因为它存在于FunctionComponent接口上)。实际情况是,您指定的是const FadeInSection的类型,而不是props的类型,所以当您以前使用它时,编译器根本不会检查您的接口

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69011232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档