如何描述一个对象类型,除了特定属性之外还可以具有附加属性?

huangapple go评论90阅读模式
英文:

How to describe an object type that can have additional properties apart from specific ones?

问题

/**

  • @typedef {object} User
  • @property {string} name - 用户姓名
  • @property {number} age - 用户年龄
  • @property {*} [from] - 用户来自哪里
  • @property {*} [to] - 用户要去哪里
    */
  1. <details>
  2. <summary>英文:</summary>
  3. I have a data type called User currently, and I want it to have two mandatory properties, name and age. In addition, users should be able to add any number of other properties to it. How should I describe it in JSDoc to support this requirement without encountering errors?

/**

  • @typedef {object} User
  • @property {string} name - user name
  • @property {number} age -user age
    */

/**

  • @type {User}
    */
    const tom={
    name:'cx',
    age:25,
    from:'sh',// error
    to:'bj',// error
    }
  1. </details>
  2. # 答案1
  3. **得分**: 1
  4. 也许你可以添加一个类似于[这里](https://jsdoc.app/tags-param.html)描述的任意类型的`{*}`属性,以消除错误。
  5. ```js
  6. /**
  7. * @typedef {Object} User
  8. * @property {string} name - 用户名(必填)
  9. * @property {number} age - 用户年龄(必填)
  10. * @property {*} [key: value] - 用户可以添加的附加属性(可选)
  11. */
  12. /**
  13. * @type {User}
  14. */
  15. const tom = {
  16. name: 'cx',
  17. age: 25,
  18. from: 'sh', // 没有错误
  19. to: 'bj', // 没有错误
  20. };

编辑:经过一些搜索,我找到了这个帖子。你能否检查是否解决了错误。

  1. /**
  2. * @typedef {object} User
  3. * @property {string} name 用户名
  4. * @property {number} age 用户年龄
  5. */
  6. /**
  7. * @typedef {Object.<string, string>} Descriptor
  8. * @typedef {User & Descriptor} UserWithDetails 带有详细信息的用户
  9. */
  10. /**
  11. * @type {UserWithDetails}
  12. */
  13. const tom = {
  14. name: "cx",
  15. age: 25,
  16. from: "sh",
  17. to: "bj",
  18. };

编辑后的编辑:如果上面的方法有效,也许你甚至可以通过以下方式简化它。

  1. /**
  2. * @typedef {object} User
  3. * @property {string} name - 用户名
  4. * @property {number} age - 用户年龄
  5. * @property {Object.<string, string>} [additionalProperties] - 附加键值对
  6. */
  7. /**
  8. * @type {User}
  9. */
  10. const tom = {
  11. name: "cx",
  12. age: 25,
  13. from: "sh",
  14. to: "bj",
  15. };
英文:

perhaps you can add a {*} property similar to the any-type described here to get rid of the error

  1. /**
  2. * @typedef {Object} User
  3. * @property {string} name - User name (mandatory)
  4. * @property {number} age - User age (mandatory)
  5. * @property {*} [key: value] - Additional properties that users can add (optional)
  6. */
  7. /**
  8. * @type {User}
  9. */
  10. const tom = {
  11. name: &#39;cx&#39;,
  12. age: 25,
  13. from: &#39;sh&#39;, // No error
  14. to: &#39;bj&#39;, // No error
  15. };

Edit: after some searching, I've found this thread. Could you check if this resolves the errors

  1. /**
  2. * @typedef {object} User
  3. * @property {string} name user name
  4. * @property {number} age user age
  5. */
  6. /**
  7. * @typedef {Object.&lt;string, string&gt;} Descriptor
  8. * @typedef {User &amp; Descriptor} UserWithDetails user with details
  9. */
  10. /**
  11. * @type {UserWithDetails}
  12. */
  13. const tom = {
  14. name: &quot;cx&quot;,
  15. age: 25,
  16. from: &quot;sh&quot;,
  17. to: &quot;bj&quot;,
  18. };

Edit on edit: If the above works perhaps you can even simplify it by doing

  1. /**
  2. * @typedef {object} User
  3. * @property {string} name - User name
  4. * @property {number} age - User age
  5. * @property {Object.&lt;string, string&gt;} [additionalProperties] - Additional key-value pairs
  6. */
  7. /**
  8. * @type {User}
  9. */
  10. const tom = {
  11. name: &quot;cx&quot;,
  12. age: 25,
  13. from: &quot;sh&quot;,
  14. to: &quot;bj&quot;,
  15. };

huangapple
  • 本文由 发表于 2023年6月15日 13:59:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定