Igor Alekseev
Igor Alekseev

Reputation: 1198

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.

Upvotes: 3

Views: 1287

Answers (1)

Nikolay Osaulenko
Nikolay Osaulenko

Reputation: 1482

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

Upvotes: 1

Related Questions