Typescript 如何正确记录此通用函数?
对于JSDocTypescript 如何正确记录此通用函数?,typescript,jsdoc,Typescript,Jsdoc,对于JSDoc@template,大括号{…}中的内容是约束,模板变量名应该放在大括号之后。因此,问题应该是: 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 commen
@template
,大括号{…}
中的内容是约束,模板变量名应该放在大括号之后。因此,问题应该是:
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
/**
*包装样式化组件以提供默认道具
*@template{{defaultProps?:Partial}}T-组件类型
*@template{any}TDefaults-默认道具
*@param{T}component-我们的样式化组件对象
*@param{TDefaults}defaultProps-对象的默认props
*@返回应用默认道具的样式化组件
*/
使用默认值导出函数(组件:T,defaultProps:TDefaults):T&{defaultProps:TDefaults}{
//eslint禁用下一行无参数重新分配
component.defaultProps=defaultProps;
//转换为任何必要的类型,因为我们无法推断样式化组件类型
返回组件(如有);
}
您还需要根据抑制错误将eslint选项
settings.jsdoc.mode
设置为typescript
。您是否使用最新版本的jsdoc插件?请参阅最新的:“eslint plugin jsdoc”:“^22.1.0”,
我个人没有使用typescript,因此我没有设置工作区来测试您的代码示例。你有你的设置。jsdoc.mode也设置为typescript吗?@DisplayNameismissing是的,我有。我仍然有以下错误:类型“T”未定义
,类型“TDefaults”未定义
,缺少jsdoc注释。
我找到了原因,请参阅:我已将模式设置为typescript,然后我不知道。您可以在github上设置一个可复制的环境post,供ppl检查
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
/**
* 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;
}