clodal
clodal

Reputation: 1825

JSDoc: How to set @param for React element as a function param?

What should I add in the JSDoc @param block in the example below for my IntelliJ Webstorm IDE to stop throwing the error: Argument Type ComponentClass<undefined> is not assignable to parameter type ??? ... `?

/**
 * Renders the passed component into a div
 * @param { ??? } component // Question: <<<
 */
const myRenderFunc = (component) => (
  <div>{component}</div>
)

Upvotes: 4

Views: 5186

Answers (2)

Oguzcan
Oguzcan

Reputation: 430

Possible solution using @typedef

/**
 * @typedef { import('./Question') } Question
 */

/**
 * Renders the passed component into a div
 * @param { Question } component
 */
const myRenderFunc = (component) => (
  <div>{component}</div>
)

or if you don't want to use @typedef

/**
 * Renders the passed component into a div
 * @param { import('./Question') } component
 */
const myRenderFunc = (component) => (
  <div>{component}</div>
)

or if it it doesn't matter that it is a Question component, just a React component, you can write React.ReactElement. Be aware that you should have React imported in the file or @typedef

/**
 * Renders the passed component into a div
 * @param { React.ReactElement } component
 */
const myRenderFunc = (component) => (
  <div>{component}</div>
)

Upvotes: 0

andr1o
andr1o

Reputation: 259

Your React component @param type should be {ReactElement}

Upvotes: 7

Related Questions