Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions types/underscore/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ declare module _ {
(value: T): boolean;
}

interface CollectionIterator<T extends TypeOfCollection<V, any>, TResult, V = Collection<T>> {
interface CollectionIterator<T extends TypeOfList<V> | TypeOfDictionary<V, any>, TResult, V = Collection<T>> {
(element: T, key: CollectionKey<V>, collection: V): TResult;
}

Expand All @@ -122,7 +122,7 @@ declare module _ {

type PropertyTypeOrAny<T, K> = K extends keyof T ? T[K] : any;

interface MemoCollectionIterator<T extends TypeOfCollection<V>, TResult, V = Collection<T>> {
interface MemoCollectionIterator<T extends TypeOfList<V> | TypeOfDictionary<V, any>, TResult, V = Collection<T>> {
(prev: TResult, curr: T, key: CollectionKey<V>, collection: V): TResult;
}

Expand All @@ -140,7 +140,7 @@ declare module _ {
: V extends Dictionary<infer T> ? T
: TDefault;

type TypeOfCollection<V, TObjectDefault = never> = TypeOfList<V> | TypeOfDictionary<V, TObjectDefault>;
type TypeOfCollection<V, TObjectDefault = never> = V extends List<any> ? TypeOfList<V> : TypeOfDictionary<V, TObjectDefault>;

type ListItemOrSelf<T> = T extends List<infer TItem> ? TItem : T;

Expand Down
38 changes: 37 additions & 1 deletion types/underscore/underscore-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,26 @@ interface AugmentedList extends _.List<StringRecord> {
notAListProperty: boolean;
}

// tslint:disable-next-line:interface-over-type-literal
type AugmentedListLiteral = {
[index: number]: StringRecord;
length: number;
notAListProperty: string;
};

interface ExplicitDictionary extends _.Dictionary<StringRecord> {
a: StringRecord;
b: StringRecord;
c: StringRecord;
}

// tslint:disable-next-line:interface-over-type-literal
type ExplicitDictionaryLiteral = {
a: StringRecord;
b: StringRecord;
c: StringRecord;
};

declare const shallowProperty: 'a';
declare const deepProperty: ['a', 'length'];
declare const matcher: Partial<StringRecord>;
Expand Down Expand Up @@ -801,6 +815,28 @@ declare const extractChainTypes: ChainTypeExtractor;

// Types

// TypeOfCollection
declare const listItem: _.TypeOfCollection<_.List<StringRecord>>;
listItem; // $ExpectType StringRecord

declare const arrayItem: _.TypeOfCollection<StringRecord[]>;
arrayItem; // $ExpectType StringRecord

declare const augmentedListItem: _.TypeOfCollection<AugmentedList>;
augmentedListItem; // $ExpectType StringRecord

declare const augmentedListLiteralItem: _.TypeOfCollection<AugmentedListLiteral>;
augmentedListLiteralItem; // $ExpectType StringRecord

declare const dictionaryItem: _.TypeOfCollection<_.Dictionary<StringRecord>>;
dictionaryItem; // $ExpectType StringRecord

declare const explicitDictionaryItem: _.TypeOfCollection<ExplicitDictionary>;
explicitDictionaryItem; // $ExpectType StringRecord

declare const explicitDictionaryLiteralItem: _.TypeOfCollection<ExplicitDictionaryLiteral>;
explicitDictionaryLiteralItem; // $ExpectType StringRecord

// Iteratee
{
// functions
Expand Down Expand Up @@ -831,7 +867,7 @@ declare const extractChainTypes: ChainTypeExtractor;
const collectionFunctionIteratee: _.Iteratee<_.Dictionary<StringRecord> | StringRecord[], string> = (element, key, collection) => {
element; // $ExpectType StringRecord
key; // $ExpectType string | number
collection; // $ExpectType Dictionary<StringRecord> | StringRecord[]
collection; // $ExpectType StringRecord[] | Dictionary<StringRecord>
return element.a;
};
collectionFunctionIteratee(recordDictionary['a'], 'a', recordDictionary); // $ExpectType string
Expand Down