【问题标题】:What's the idiomatic why to document the types in a Vuex state definition?为什么要在 Vuex 状态定义中记录类型的惯用语是什么?
【发布时间】:2020-08-21 06:52:24
【问题描述】:

我们在一个组件中有一个相当大的状态,代码是这样的

export default {
  someObject: {},
  someOtherObject: {},
  evenMore: []
  <etc...>
}

没有任何类型,很难弄清楚发生了什么。我们目前不能使用 Typescript。是否有惯用的使用方式,例如JSDoc 来记录类型?类似的东西

export default {
  /** @var SomeClass */
  someObject: {},
  /** @var SomeOtherClass
  someOtherObject: {},
  /** String[] */
  evenMore: []
  <etc...>
}

还是太吵了? Vue 的标准做法是什么?

编辑:这是一个例子:https://www.npmjs.com/package/jsdoc-vuex-plugin#the-state

/**
 * The Vuex 'state' object.
 * @name State
 * @type {object} 
 * @property {boolean} boolProp This property is a boolean.
 * @property {string} strProp This property is a string.
 * @property {number} numProp This property is a number.
 */

【问题讨论】:

  • 就我个人而言,我使用 jsdoc 进行编码,因为它与 vscode 配合得很好。虽然 @property@typedef 在 vscode 中效果不佳
  • @A.Lau 你能举个例子吗?

标签: vue.js types vuex documentation jsdoc


【解决方案1】:

这或多或少取决于您使用的 IDE。我使用的是VSCode,它对类型的理解非常好,我只需要在某些情况下显式地制作jsdoc cmets。

所以使用导出默认值,而不是像这样导出对象:

export default {
  /** @var SomeClass */
  someObject: {},
  /** @var SomeOtherClass
  someOtherObject: {},
  /** String[] */
  evenMore: []
  <etc...>
}

你需要做的:

const export_me = {
    /** @var SomeClass */
    someObject: {},
    /** @var SomeOtherClass
    someOtherObject: {},
    /** String[] */
    evenMore: []
  }

export default export_me

对于函数,如果你不在参数中解构它们,你将需要做这样的事情

/** 
  @typedef {{
    name: string,
    age: number
  }} Person
  @param {Person} person
  @param {number} speed

  @returns {number}
*/
export function runningMan (person, speed) {
    const {
        name,
        age
    } = person;
}

我不使用@property,因为它在 vscode 中不起作用。

【讨论】:

  • 谢谢!你对我的编辑有意见吗?此链接:npmjs.com/package/jsdoc-vuex-plugin
  • 如果你想问这类问题,Vue 是不和谐的。寻找 Vue Land
  • 我已经在使用 IRC 频道了。但聊天不会创造“googleability”。 :)
猜你喜欢
  • 2011-11-03
  • 2021-12-08
  • 1970-01-01
  • 2018-10-26
  • 2021-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-12
相关资源
最近更新 更多