代码之家  ›  专栏  ›  技术社区  ›  noblerare

typescript-键入react组件时出现Intrinsicattributes和子类型问题

  •  1
  • noblerare  · 技术社区  · 7 年前

    我正在将响应文件迁移到 .tsx 也遇到了这个问题。

    页脚.tsx

    const Footer = ({ children, inModal }) => (
        <footer className={'(inModal ? " in-modal" : "") }>
            <div>
                {children}
            </div>
        </footer>
    );
    
    export default Footer;
    

    父组件.tsx

    import Footer from 'path/to/Footer';
    
    export class ParentComponent extends React.Component<{ showSomething: (() => void) }> {
        render() {
            return (
                <Footer>
                    <Button onClick={() => this.props.showSomething()}>Add</Button>
                </Footer>
            );
        }
    }
    

    下面有一个红色的下划线 <Footer> 标记错误:

    [ts]类型“children:element;”不能分配给类型 'Intrinsicattributes&children:any;inmodal:any;'。类型'。{ children:element;'不能分配给类型'children:any; 模态:任意;'。 类型“children:element;”中缺少属性“inmodal”。

    我不太清楚该如何解读这意味着什么。任何帮助都将不胜感激。

    1 回复  |  直到 7 年前
        1
  •  3
  •   CRice    7 年前

    Footer inModal

    const Footer = ({ children, inModal = false }) => ...
    

    const Footer = ({ children, inModal }: {children: any, inModal?: boolean}) => ...
    

    <Footer inModal={false}> ... </Footer>
    
    推荐文章