Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you write JSDoc in Javascript for TypeScript Compiler?

I know that there is ability to check JS code with TS compiler. And I know that TS compiler understand JSDoc comments (https://github.com/Microsoft/TypeScript/wiki/JSDoc-support-in-JavaScript).

I want to use TS compiler with my React project and I don't know how specify props definitions with JSDoc :(

I've tried

class PropertyDetails extends React.Component {
  static propTypes = {
    breadcrumbChange: PropTypes.func,
    folderId: PropTypes.string.isRequired
  }

  /**
   * @typedef {Object} Props
   * @property {function} breadcrumbChange
   * @property {string} folderId
   */

  /**
   * @constructor
   * @param {Props} props
   */
  constructor(props) {
    super(props);

but it doesn't work.

like image 735
Igor Alekseev Avatar asked May 29 '26 13:05

Igor Alekseev


1 Answers

To make IntelliSense and type checking working you have to specify prop types for Component class since in typings it is described like generic object. So if you try something like this

/**
 * @typedef {Object} Props
 * @prop {Function} breadcrumbChange
 * @prop {string} folderId
 * @extends React.Component<Props>
 */
class PropertyDetails extends React.Component {
}

it should work

like image 98
Nikolay Osaulenko Avatar answered Jun 01 '26 03:06

Nikolay Osaulenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!