如何在JSDoc中记录父类动态添加的继承属性?
解决Stimulus控制器动态属性的JSDoc类型提示问题
针对父类动态添加的clearResultValue属性,无需重新定义属性本身的前提下,有几种可行的JSDoc写法能让IDE识别类型并提供检查:
写法一:结合
@augments和@property声明
在类的注释块里明确继承关系并标注属性类型,让IDE关联父类的同时识别动态属性:/** * @augments {Controller} * @property {boolean} clearResultValue */ import { Controller } from '@hotwired/stimulus'; export default class extends Controller {}写法二:用交叉类型标注
@this
通过@this把父类类型和自定义属性类型合并,直接指定当前实例的类型结构:import { Controller } from '@hotwired/stimulus'; /** * @this {Controller & { clearResultValue: boolean }} */ export default class extends Controller {}局部类型标注(适合特定方法内)
如果只在某个方法里需要类型提示,直接在使用属性前标注类型:import { Controller } from '@hotwired/stimulus'; export default class extends Controller { handleAction() { /** @type {boolean} */ const result = this.clearResultValue; // 此时IDE会对result做布尔类型检查 } }
内容的提问来源于stack exchange,提问作者gremo
相关产品推荐
相关产品推荐

