admin管理员组文章数量:1208155
I am migrating my React files to .tsx
and have come across this issue.
Footer.tsx
const Footer = ({ children, inModal }) => (
<footer className={'(inModal ? " in-modal" : "") }>
<div>
{children}
</div>
</footer>
);
export default Footer;
ParentComponent.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>
);
}
}
There is a red underline underneath the <Footer>
tag with the error:
[ts] Type '{ children: Element; }' is not assignable to type 'IntrinsicAttributes & { children: any; inModal: any; }'. Type '{ children: Element; }' is not assignable to type '{ children: any; inModal: any; }'. Property 'inModal' is missing in type '{ children: Element; }'.
I'm not quite sure how to decipher what that means. Any help would be greatly appreciated.
I am migrating my React files to .tsx
and have come across this issue.
Footer.tsx
const Footer = ({ children, inModal }) => (
<footer className={'(inModal ? " in-modal" : "") }>
<div>
{children}
</div>
</footer>
);
export default Footer;
ParentComponent.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>
);
}
}
There is a red underline underneath the <Footer>
tag with the error:
[ts] Type '{ children: Element; }' is not assignable to type 'IntrinsicAttributes & { children: any; inModal: any; }'. Type '{ children: Element; }' is not assignable to type '{ children: any; inModal: any; }'. Property 'inModal' is missing in type '{ children: Element; }'.
I'm not quite sure how to decipher what that means. Any help would be greatly appreciated.
Share Improve this question edited Jul 3, 2018 at 19:45 noblerare asked Jul 3, 2018 at 19:04 noblerarenoblerare 11.9k26 gold badges91 silver badges164 bronze badges 3 |2 Answers
Reset to default 15The error is indicating that the Footer
component requires a prop inModal
to be passed to it. To fix the issue, you can either:
Give it a default value:
const Footer = ({ children, inModal = false }) => ...
Tell typescript that it's optional:
const Footer = ({ children, inModal }: {children: any, inModal?: boolean}) => ...
Or explicitly provide that prop whenever you use the footer:
<Footer inModal={false}> ... </Footer>
I see your code and find you have not specify the type of inModal props, I believe it’s a good prectice to define interface like that:
export class ParentComponent extends React.Component<{ showSomething: (() => void) }> {
you can modify this something like this:
interface FooterPropsType{
showSomething?:()=> void,
isModal?: boolean,
}
export class ParentComponent extends React.Component<FooterPropsType> {
本文标签:
版权声明:本文标题:javascript - Typescript - IntrinsicAttributes and children type issues when typing React components - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738737315a2109644.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
inModal
a default value offalse
in yourFooter
component?const Footer = ({ children, inModal = false }) ...
– Tholle Commented Jul 3, 2018 at 19:07