I've been trying to document my JS code with types, but am having trouble with this one function. I'm trying to filter an array using its type property, however when trying to map the provided string parameter to type using the typedef-ed object Filter
, the return type is a union type (CardItem | StickyNoteItem)[]
whereas I expected it to be CardItem[]
.
When I replace the T in the @returns
with a string literal "card", the only type returned is CardItem[]
I have tried to use @template
but it doesn't seem to make the parameter conform to the union type.
How do I fix this so that the @return
type matches the string parameter?
/**
*
* @typedef {{card: CardItem, sticky_note: StickyNoteItem}} Filters
* @typedef {keyof Filters} T
* @param {WidgetItem[]} items
* @param {T} widgetType
* @returns {Filters[T][]}
*/
export function filterItems(items, widgetType) {
return items.filter(({type}) => type === widgetType)
}
Two things: first you indeed need to use the @template
tag when defining generics; and second, defining a @typedef
inside the same block as the one documenting the function doesn't seem supported.
You can get the expected return type like this:
/**
* @typedef {{ card: CardItem, sticky_note: StickyNoteItem }} Filters
*/
/**
* @template {keyof Filters} T
* @param {WidgetItem[]} items
* @param {T} widgetType
* @returns {Filters[T][]}
*/