Skip to content

Commit

Permalink
Solve dumb CC type inference bug (ampproject#34355)
Browse files Browse the repository at this point in the history
  • Loading branch information
rcebulko authored and rochapablo committed Aug 30, 2021
1 parent 86ec6fb commit 38b5017
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/core/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@ export const {isArray} = Array;
/**
* If the specified argument is an array, it's returned as is. If it's a
* single item, the array containing this item is created and returned.
* @param {!Array<T>|T} arrayOrSingleItem
* @return {!Array<T>}
*
* The double-template pattern here solves a bug where CC can be passed a value
* with declared type {string|!Array<string>} and return a value with a type of
* {!Array<string|Array<string>>}.
*
* @param {!Array<T>|S} arrayOrSingleItem
* @return {!Array<T>|!Array<S>}
* @template S
* @template T
*/
export function arrayOrSingleItemToArray(arrayOrSingleItem) {
return isArray(arrayOrSingleItem)
? /** @type {!Array<T>} */ (arrayOrSingleItem)
: [arrayOrSingleItem];
: [/** @type {!S} */ (arrayOrSingleItem)];
}

/**
Expand Down

0 comments on commit 38b5017

Please sign in to comment.