【问题标题】:How can I properly document this generic function?如何正确记录此通用功能?
【发布时间】:2020-04-01 12:18:09
【问题描述】:
/**
 * Wraps a styled component to supply default props
 * @template {T} - Component type
 * @template {TDefaults} - Default props
 * @param {T} component  - Our styled component object
 * @param {TDefaults} defaultProps - The object's default props
 * @returns the styled component with default props applied
 */
export function withDefault<T extends { defaultProps?: Partial<TDefaults> }, TDefaults>(component: T, defaultProps: TDefaults): T & { defaultProps: TDefaults } {
  // eslint-disable-next-line no-param-reassign
  component.defaultProps = defaultProps;
  // Cast to any necessary as we can't infer styled component type
  return component as any;
}

这会返回这些错误:

  5:0    warning  The type 'T' is undefined          jsdoc/no-undefined-types
  6:0    warning  The type 'TDefaults' is undefined  jsdoc/no-undefined-types
  9:41   warning  Missing JSDoc comment              jsdoc/require-jsdoc
  9:135  warning  Missing JSDoc comment              jsdoc/require-jsdoc

【问题讨论】:

  • 您使用的是最新版本的 jsdoc 插件吗?见github.com/gajus/eslint-plugin-jsdoc/issues/428
  • 是的,最新消息:"eslint-plugin-jsdoc": "^22.1.0",
  • 我个人不使用打字稿,所以我没有设置工作区来测试您的代码示例。您也将settings.jsdoc.mode 设置为打字稿?
  • @DisplayNameismissing 是的,我愿意。

标签: typescript jsdoc


【解决方案1】:

对于JSDoc@template,大括号{...}里面的东西是约束,模板变量名应该放在大括号后面。所以correct syntax 应该是:

/**
 * Wraps a styled component to supply default props
 * @template {{ defaultProps?: Partial<TDefaults> }} T - Component type
 * @template {any} TDefaults - Default props
 * @param {T} component  - Our styled component object
 * @param {TDefaults} defaultProps - The object's default props
 * @returns the styled component with default props applied
 */
export function withDefault<T extends { defaultProps?: Partial<TDefaults> }, TDefaults>(component: T, defaultProps: TDefaults): T & { defaultProps: TDefaults } {
  // eslint-disable-next-line no-param-reassign
  component.defaultProps = defaultProps;
  // Cast to any necessary as we can't infer styled component type
  return component as any;
}

还需要根据this doc将eslint选项settings.jsdoc.mode设置为typescript来抑制报错。

【讨论】:

  • 我还是有以下错误:The type 'T' is undefined,The type 'TDefaults' is undefined,Missing JSDoc comment.
  • 我找到原因了,见:npmjs.com/package/eslint-plugin-jsdoc#mode
  • 我已将模式设置为 Typescript
  • 那我就不知道了。您可以在 github 上设置一个可重现的 env 以供大家检查
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-19
  • 1970-01-01
  • 2020-07-15
  • 1970-01-01
  • 2021-12-30
  • 2021-08-24
  • 1970-01-01
相关资源
最近更新 更多