diff --git a/types/underscore/index.d.ts b/types/underscore/index.d.ts index 9c05c67d9ad581..b5e7d001f2feb1 100644 --- a/types/underscore/index.d.ts +++ b/types/underscore/index.d.ts @@ -706,94 +706,79 @@ declare module _ { **********/ /** - * Returns the first element of an array. Passing n will return the first n elements of the array. - * @param array Retrieves the first element of this array. - * @return Returns the first element of `array`. - **/ - first(array: _.List): T | undefined; - - /** - * @see _.first - * @param n Return more than one element from `array`. - **/ - first( - array: _.List, - n: number): T[]; - - /** - * @see _.first - **/ - head(array: _.List): T | undefined; - - /** - * @see _.first - **/ - head( - array: _.List, - n: number): T[]; - - /** - * @see _.first - **/ - take(array: _.List): T; + * Returns the first element of `list`. Passing `n` will return the + * first `n` elements of `list`. + * @param list The list to retrieve elements from. + * @param n The number of elements to retrieve, optional. + * @returns The first `n` elements of `list` or the first element if + * `n` is omitted. + **/ + first>(list: V): TypeOfList | undefined; + first>( + list: V, + n: number + ): TypeOfList[]; /** - * @see _.first - **/ - take( - array: _.List, - n: number): T[]; + * @see first + **/ + head: UnderscoreStatic['first']; /** - * Returns everything but the last entry of the array. Especially useful on the arguments object. - * Pass n to exclude the last n elements from the result. - * @param array Retrieve all elements except the last `n`. - * @param n Leaves this many elements behind, optional. - * @return Returns everything but the last `n` elements of `array`. - **/ - initial( - array: _.List, - n?: number): T[]; + * @see first + **/ + take: UnderscoreStatic['first']; /** - * Returns the last element of an array. Passing n will return the last n elements of the array. - * @param array Retrieves the last element of this array. - * @return Returns the last element of `array`. - **/ - last(array: _.List): T | undefined; + * Returns everything but the last entry of `list`. Especially useful + * on the arguments object. Pass `n` to exclude the last + * `n` elements from the result. + * @param list The list to retrieve elements from. + * @param n The number of elements from the end of `list` to omit, + * optional, default = 1. + * @returns The elements of `list` with the last `n` items omitted. + **/ + initial>( + list: V, + n?: number + ): TypeOfList[]; /** - * @see _.last - * @param n Return more than one element from `array`. - **/ - last( - array: _.List, - n: number): T[]; + * Returns the last element of `list`. Passing `n` will return the last + * `n` elements of `list`. + * @param list The list to retrieve elements from. + * @param n The number of elements to retrieve, optional. + * @returns The last `n` elements of `list` or the last element if `n` + * is omitted. + **/ + last>(list: V): TypeOfList | undefined; + last>( + list: V, + n: number + ): TypeOfList[]; /** - * Returns the rest of the elements in an array. Pass an index to return the values of the array - * from that index onward. - * @param array The array to retrieve all but the first `index` elements. - * @param n The index to start retrieving elements forward from, optional, default = 1. - * @return Returns the elements of `array` from `index` to the end of `array`. - **/ - rest( - array: _.List, - n?: number): T[]; + * Returns the rest of the elements in `list`. Pass an `index` to + * return the values of the list from that index onward. + * @param list The list to retrieve elements from. + * @param index The index to start retrieving elements from, optional, + * default = 1. + * @returns The elements of `list` from `index` to the end of the list. + **/ + rest>( + list: V, + index?: number + ): TypeOfList[]; /** - * @see _.rest - **/ - tail( - array: _.List, - n?: number): T[]; + * @see rest + **/ + tail: UnderscoreStatic['rest']; /** - * @see _.rest - **/ - drop( - array: _.List, - n?: number): T[]; + * @see rest + **/ + drop: UnderscoreStatic['rest']; /** * Returns a copy of the array with all falsy values removed. In JavaScript, false, null, 0, "", @@ -4368,70 +4353,65 @@ declare module _ { **********/ /** - * Wrapped type `any[]`. - * @see _.first - **/ + * Returns the first element of the wrapped list. Passing `n` will + * return the first `n` elements of the wrapped list. + * @param n The number of elements to retrieve, optional. + * @returns The first `n` elements of the wrapped list or the first + * element if `n` is omitted. + **/ first(): T | undefined; - - /** - * Wrapped type `any[]`. - * @see _.first - **/ first(n: number): T[]; /** - * @see _.first - **/ - head(): T | undefined; - - /** - * @see _.first - **/ - head(n: number): T[]; - - /** - * @see _.first - **/ - take(): T; + * @see first + **/ + head: Underscore['first']; /** - * @see _.first + * @see first **/ - take(n: number): T[]; + take: Underscore['first']; /** - * Wrapped type `any[]`. - * @see _.initial - **/ + * Returns everything but the last entry of the wrapped list. + * Especially useful on the arguments object. Pass `n` to exclude the + * last `n` elements from the result. + * @param n The number of elements from the end of the wrapped list to + * omit, optional, default = 1. + * @returns The elements of the wrapped list with the last `n` items + * omitted. + **/ initial(n?: number): T[]; /** - * Wrapped type `any[]`. - * @see _.last - **/ + * Returns the last element of the wrapped list. Passing `n` will + * return the last `n` elements of the wrapped list. + * @param n The number of elements to retrieve, optional. + * @returns The last `n` elements of the wrapped list or the last + * element if `n` is omitted. + **/ last(): T | undefined; - - /** - * Wrapped type `any[]`. - * @see _.last - **/ last(n: number): T[]; /** - * Wrapped type `any[]`. - * @see _.rest - **/ + * Returns the rest of the elements in the wrapped list. Pass an + * `index` to return the values of the list from that index onward. + * @param index The index to start retrieving elements from, optional, + * default = 1. + * @returns The elements of the wrapped list from `index` to the end + * of the list. + **/ rest(n?: number): T[]; /** - * @see _.rest - **/ - tail(n?: number): T[]; + * @see rest + **/ + tail: Underscore['rest']; /** - * @see _.rest - **/ - drop(n?: number): T[]; + * @see rest + **/ + drop: Underscore['rest']; /** * Wrapped type `any[]`. @@ -5370,70 +5350,65 @@ declare module _ { **********/ /** - * Wrapped type `any[]`. - * @see _.first - **/ + * Returns the first element of the wrapped list. Passing `n` will + * return the first `n` elements of the wrapped list. + * @param n The number of elements to retrieve, optional. + * @returns A chain wrapper around the first `n` elements of the + * wrapped list or around the first element if `n` is omitted. + **/ first(): _ChainSingle; + first(n: number): _Chain; /** - * Wrapped type `any[]`. - * @see _.first - **/ - first(n: number): _Chain; - - /** - * @see _.first - **/ - head(): _Chain; - - /** - * @see _.first - **/ - head(n: number): _Chain; - - /** - * @see _.first - **/ - take(): _Chain; - - /** - * @see _.first - **/ - take(n: number): _Chain; + * @see first + **/ + head: _Chain['first']; /** - * Wrapped type `any[]`. - * @see _.initial - **/ - initial(n?: number): _Chain; + * @see first + **/ + take: _Chain['first']; /** - * Wrapped type `any[]`. - * @see _.last - **/ - last(): _ChainSingle; + * Returns everything but the last entry of the wrapped list. + * Especially useful on the arguments object. Pass `n` to exclude the + * last `n` elements from the result. + * @param n The number of elements from the end of the wrapped list to + * omit, optional, default = 1. + * @returns A chain wrapper around the elements of the wrapped list + * with the last `n` items omitted. + **/ + initial(n?: number): _Chain; /** - * Wrapped type `any[]`. - * @see _.last - **/ - last(n: number): _Chain; + * Returns the last element of the wrapped list. Passing `n` will + * return the last `n` elements of the wrapped list. + * @param n The number of elements to retrieve, optional. + * @returns A chain wrapper around the last `n` elements of the wrapped + * list or around the last element if `n` is omitted. + **/ + last(): _ChainSingle; + last(n: number): _Chain; /** - * Wrapped type `any[]`. - * @see _.rest - **/ - rest(n?: number): _Chain; + * Returns the rest of the elements in the wrapped list. Pass an + * `index` to return the values of the list from that index onward. + * @param index The index to start retrieving elements from, optional, + * default = 1. + * @returns A chain wrapper around the elements of the wrapped list + * from `index` to the end of the list. + **/ + rest(n?: number): _Chain; /** - * @see _.rest - **/ - tail(n?: number): _Chain; + * @see rest + **/ + tail: _Chain['rest']; /** - * @see _.rest - **/ - drop(n?: number): _Chain; + * @see rest + **/ + drop: _Chain['rest']; /** * Wrapped type `any[]`. diff --git a/types/underscore/underscore-tests.ts b/types/underscore/underscore-tests.ts index 6bf559ea5d7999..4d33ad0f255be6 100644 --- a/types/underscore/underscore-tests.ts +++ b/types/underscore/underscore-tests.ts @@ -1634,6 +1634,98 @@ undefinedIdentityIterateeResult; // $ExpectType StringRecord // Arrays +// first, head, take +{ + // without n - first + _.first(stringRecordList); // $ExpectType StringRecordOrUndefined + _(stringRecordList).first(); // $ExpectType StringRecordOrUndefined + extractChainTypes(_.chain(stringRecordList).first()); // $ExpectType ChainType + + // without n - head + _.head(stringRecordList); // $ExpectType StringRecordOrUndefined + _(stringRecordList).head(); // $ExpectType StringRecordOrUndefined + extractChainTypes(_.chain(stringRecordList).head()); // $ExpectType ChainType + + // without n - take + _.take(stringRecordList); // $ExpectType StringRecordOrUndefined + _(stringRecordList).take(); // $ExpectType StringRecordOrUndefined + extractChainTypes(_.chain(stringRecordList).take()); // $ExpectType ChainType + + // with n - first + _.first(stringRecordList, simpleNumber); // $ExpectType StringRecord[] + _(stringRecordList).first(simpleNumber); // $ExpectType StringRecord[] + extractChainTypes(_.chain(stringRecordList).first(simpleNumber)); // $ExpectType ChainType + + // with n - head + _.head(stringRecordList, simpleNumber); // $ExpectType StringRecord[] + _(stringRecordList).head(simpleNumber); // $ExpectType StringRecord[] + extractChainTypes(_.chain(stringRecordList).head(simpleNumber)); // $ExpectType ChainType + + // with n - take + _.take(stringRecordList, simpleNumber); // $ExpectType StringRecord[] + _(stringRecordList).take(simpleNumber); // $ExpectType StringRecord[] + extractChainTypes(_.chain(stringRecordList).take(simpleNumber)); // $ExpectType ChainType +} + +// initial +{ + // without n + _.initial(stringRecordList); // $ExpectType StringRecord[] + _(stringRecordList).initial(); // $ExpectType StringRecord[] + extractChainTypes(_.chain(stringRecordList).initial()); // $ExpectType ChainType + + // with n + _.initial(stringRecordList, simpleNumber); // $ExpectType StringRecord[] + _(stringRecordList).initial(simpleNumber); // $ExpectType StringRecord[] + extractChainTypes(_.chain(stringRecordList).initial(simpleNumber)); // $ExpectType ChainType +} + +// last +{ + // without n + _.last(stringRecordList); // $ExpectType StringRecordOrUndefined + _(stringRecordList).last(); // $ExpectType StringRecordOrUndefined + extractChainTypes(_.chain(stringRecordList).last()); // $ExpectType ChainType + + // with n + _.last(stringRecordList, simpleNumber); // $ExpectType StringRecord[] + _(stringRecordList).last(simpleNumber); // $ExpectType StringRecord[] + extractChainTypes(_.chain(stringRecordList).last(simpleNumber)); // $ExpectType ChainType +} + +// rest, tail, drop +{ + // without n - rest + _.rest(stringRecordList); // $ExpectType StringRecord[] + _(stringRecordList).rest(); // $ExpectType StringRecord[] + extractChainTypes(_.chain(stringRecordList).rest()); // $ExpectType ChainType + + // without n - tail + _.tail(stringRecordList); // $ExpectType StringRecord[] + _(stringRecordList).tail(); // $ExpectType StringRecord[] + extractChainTypes(_.chain(stringRecordList).tail()); // $ExpectType ChainType + + // without n - drop + _.drop(stringRecordList); // $ExpectType StringRecord[] + _(stringRecordList).drop(); // $ExpectType StringRecord[] + extractChainTypes(_.chain(stringRecordList).drop()); // $ExpectType ChainType + + // with n - rest + _.rest(stringRecordList, simpleNumber); // $ExpectType StringRecord[] + _(stringRecordList).rest(simpleNumber); // $ExpectType StringRecord[] + extractChainTypes(_.chain(stringRecordList).rest(simpleNumber)); // $ExpectType ChainType + + // with n - tail + _.tail(stringRecordList, simpleNumber); // $ExpectType StringRecord[] + _(stringRecordList).tail(simpleNumber); // $ExpectType StringRecord[] + extractChainTypes(_.chain(stringRecordList).tail(simpleNumber)); // $ExpectType ChainType + + // with n - drop + _.drop(stringRecordList, simpleNumber); // $ExpectType StringRecord[] + _(stringRecordList).drop(simpleNumber); // $ExpectType StringRecord[] + extractChainTypes(_.chain(stringRecordList).drop(simpleNumber)); // $ExpectType ChainType +} + // flatten { // one dimension, deep