skip to Main Content

I’m getting a JSDoc syntax error pointing to the first line of my JSDoc comment where it says /**. Not sure what else I need to fix.

/**
 * Gets any field value from a user's User collection.
 * @param {number} IDType 0 = uid, 1 = username.
 * @param {string} inputID the input ID, based on the IDType.
 * @param {string} desiredField the desired field value.
 * @returns {number, string, string} 1/0, error message, desired info.
 */
async function getUserInfo(
    IDType: number
    , inputID: string
    , desiredField: string
): Promise<[number, string, string]> {
 //...

3

Answers


  1. Just install JSDoc Lint… while three return values appear strange to me:

    @returns {number, string, string} 1/0, error message, desired info.
    

    And I’m probably right, because the documentation doesn’t use any comma.

    @returns {number|string} 1/0, error message or desired info.
    
    Login or Signup to reply.
  2. Probably your return type is too complex, try with just Promise as documentation shows.

    /**
     * ...
     * @returns {Promise} 1/0, error message, desired info.
     */
    
    Login or Signup to reply.
  3. The type statement {number, string, string} on the @returns line is a syntax error.

    As your function is async, the type should be defined as {Promise<ResultType>}. Additionally, because you are returning a tuple, you should be wrapping number, string, string in square brackets – [number, string, string].

    This results in a declaration of:

    /**
     * Gets any field value from a user's User collection.
     * @param {number} IDType 0 = uid, 1 = username.
     * @param {string} inputID the input ID, based on the IDType.
     * @param {string} desiredField the desired field value.
     * @returns {Promise<[number, string, string]>} 1/0, error message, desired info.
     */
    async function getUserInfo(
        IDType: number
        , inputID: string
        , desiredField: string
    ): Promise<[number, string, string]> {
      //...
    }
    

    Alternatively, you can also define a type for your tuple and restrict the acceptable values for the IDType parameter:

    type IDType = 0 | 1;
    type GetUserInfoResult = [ IDType, string, string ];
    
    /**
     * Gets any field value from a user's User collection.
     * @param {IDType} typeOfInputID 0 = uid, 1 = username.
     * @param {string} inputID the input ID, of the given type.
     * @param {string} desiredField the desired field value.
     * @returns {Promise<GetUserInfoResult>} 1/0, error message, desired info.
     */
    async function getUserInfo(
        typeOfInputID: IDType // renamed to prevent type conflict
        , inputID: string
        , desiredField: string
    ): Promise<GetUserInfoResult> {
      //...
    }
    

    This allows you to destructure the result and maintain the restrictive types:

    const result = await getUserInfo(1, 'exampleUser', 'displayName');
    // type of result:        GetUserInfoResult
    
    const [typeOfID, inputId, desiredField] = result;
    // type for typeOfID:      IDType (accepts only 0 or 1)
    // type for inputId:       string
    // type for desiredField:  string
    

    You can also destructure the result directly and still preserve the types:

    const [typeOfID, inputId, desiredField] = await getUserInfo(1, 'exampleUser', 'displayName');
    // type for typeOfID:      IDType (accepts only 0 or 1)
    // type for inputId:       string
    // type for desiredField:  string
    

    With the tuple type, you can also name each value in the tuple. These names are only used in your IDE and have no effect on the use of the value in JavaScript.

    type GetUserInfoResult = [ typeOfID: IDType, errorMsg: string, strValue: string ];
    
    const result = await getUserInfo(1, 'exampleUser', 'displayName');
    result[0] // type shows as "IDType (typeOfID)"
    result[1] // type shows as "string (errorMsg)"
    result[2] // type shows as "string (strValue)"
    
    const [typeOfID, inputId, desiredField] = result;
    // type for typeOfID shows as "IDType"
    // type for inputId shows as "string"
    // type for desiredField shows as "string"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search