簡體   English   中英

JSDOC @property of custom object - 如何獲取可以具有任何名稱但必須是具有指定道具的對象值的屬性的自動建議?

[英]JSDOC @property of custom object - how to get autosuggestions for property that can have any name but must be a value of object with specified props?

我正在為表單編寫自定義 React 掛鈎,並且由於它將具有深層嵌套結構,因此我想使用 JSDOC 注釋獲得自動建議(並且不知道打字稿,而且我的團隊也使用純 JS)。 但我有一個問題。 一切都按預期工作,直到最后一個嵌套,在那里我需要傳遞屬性名稱是動態的數據對象(這些是表單中字段的名稱,將是掛鈎和自定義表單組件將返回給我的數據對象的名稱 - 所以他們絕對必須是動態的)——並且這些屬性必須是對象類型——現在我將定義其屬性——然后應該只提供那些屬性——但是自動建議不起作用,因為它不將 * 識別為“包羅萬象”。 .. 無論如何,經過數小時的努力使其發揮作用后,我放棄了並決定在此處發布問題,希望你們中的一些人可能知道如何使其發揮作用(如果可能的話)。 無論如何,感謝您的寶貴時間,您可以在下面看到我的代碼和示例用法:

PS 我使用 VS Code 和其他領域的自動建議工作得很好,正如我提到的,當我命名道具而不是 * 時它也有效 - 但我所有嘗試在那里放置動態值的嘗試都失敗了......

/**
 * @typedef {Object} FormField
 * @property {string} initialValue - The initial value of the field
 * @property {string} type - The type of the field
 * @property {function} validate - The validation function for the field
 * @property {string} errorMessage - Error message to display if input is wrong
 * ... TODO add more here later ...
 */

/**
 * @typedef {Object} Data
 * @property {FormField} * - A property with any name, and value of an FormField object {@link FormField}
 */

// The line above this is where the problem is - * is not recognized - when I put any
// other name and I put that name when calling it - autosuggestion works

/**
 * @typedef {Object} Item
 * @property {string} [title] - The title to display above the input fields - optional
 * @property {string} [className] - Class name to give to container for all fields
 * @property {Data} data - The data objects specifying just how to create the form. Use
 * autosuggestion to properly set it up
 */

/**
 * @description A custom hook for handling forms
 * @param {Item[]} arr - An array of item objects - from which to create a form
 * @return {Object} - Object with form state, data and functions
 */

// Example usage:
const form = useForm([
  {
    title: 'Test',
    className: 'test',
    data: {
      email: {initialValue: '', type: 'email', validate: value => isValidEmail(value), errorMessage: 'Invalid email address'},
      anyNameHere: {} // Should be able to take any name here and give me initialValue, validate and other
// suggestions when I trigger autosuggestions.
    }
  }
]);

如果我沒看錯, data字段可能有任何值為FormField的鍵,因此您可以將Data定義為

/**
 * @typedef {Record<string, FormField>} Data
 */

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM