0

I'm trying to define an Object using JSDoc typedefs. So given the following type definition:

/**
 * @typedef AccommodationData
 * @type {Object}
 * @property {string} id
 * @property {string} name
 * @property {boolean} selected
 */

I would define the type of accommodations. Then I want to be able to iterate over this using the map method and the spread operator to keep things concise.

import { LightningElement } from 'lwc';

export default class AccommodationList extends LightningElement {
    // --- Private properties ---

    /** @type {AccommodationData[]} */
    accommodations = [];

    // --- Getters ---
    
    /**
     * Get Accommodation Data with applied selections.
     * @returns {AccommodationData[]}
     */
    get accommodationsData() {
        return this.accommodations.map((a) => ({
            ...a,
            selected: this.selectedId === a.id
        }));
    }
}

When I do this, my IDE gives me the following warning:

Returned expression type (any & {selected: boolean})[] is not assignable to type AccommodationData[]   Type any & {selected: boolean} is not assignable to type AccommodationData     Type number is not assignable to type undefined

Is there a way to use this typedef with this method and not throw the warning from the IDE? (Error pictured below)

enter image description here

2
  • Could you provide code for class which use get accommodationsData because looks like this.accommodations has type any Commented Mar 4, 2023 at 20:16
  • @maksimr I updated the question with more of the class. I also added a screen grab of the warning/error I'm seeing. Commented Mar 6, 2023 at 15:45

1 Answer 1

0

The answer is to make these properties optional. Otherwise, they are required whenever defining a new instance of the Type.

    /**
     * @typedef AccommodationData
     * @type {Object}
     * @property {string} [id]
     * @property {string} [name]
     * @property {boolean} [selected]
     */
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.