Type '{}' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes

后端 未结 7 2989
梦如初夏
梦如初夏 2020-12-15 02:42

i am currently making a simple react application. this is my index.tsx

import * as React from \'react\';
import * as ReactDOM from \'react-dom\         


        
7条回答
  •  心在旅途
    2020-12-15 03:08

    All you need is to declare the component type properly to include the props type:

    interface IMyProps {
        myValue: boolean,
    }
    
    const MyComponent: React.FC = (props: IMyProps) => {
        ...
    }
    
    export default MyComponent;
    

    Then you can use it as:

    import MyComponent from '../MyComponent';
    
    ...
    
    return 
    

    And voila, typescript is happy. The good thing about it is that typescript is now checking for passing only the parameters they actually exist in the props interface (can prevent typos and so on).

    For the standard component it would be something similar to (what's already in Swapnill's example):

    class MyComponent extends React.Component{
        constructor(props: IMyProps){}
    }
    export default MyComponent;
    

提交回复
热议问题