How to use typescript jsdoc annotations for React PropTypes

后端 未结 3 725
悲&欢浪女
悲&欢浪女 2021-02-07 14:22

When defining react components using typescript we can write something like:

class SomeComponent extends React.Component {
         


        
3条回答
  •  太阳男子
    2021-02-07 15:02

    This works, though it may not be so good.

    // Foo.jsx
    import * as React from 'react';
    
    /**
     * @type {{ new(props: any): {
         props: { a: string, b: number },
         state: any,
         context: any,
         refs: any,
         render: any,
         setState: any,
         forceUpdate: any
       } }}
     */
    const Foo = class Foo extends React.Component {
      render() {
        return 
    {this.props.b}
    ; } }; export default Foo; // import Foo and use it in .tsx or .jsx file import Foo from './Foo'; ; // error: Type '{}' is not assignable to type '{ a: string; b: number; }' ; // OK

提交回复
热议问题