From 1e3835e65e25802c16eb82796ca11f34e4d1fa9e Mon Sep 17 00:00:00 2001 From: Andrei Alecu Date: Wed, 6 Jan 2016 19:25:44 +0200 Subject: [PATCH] underscore: fix chain with Dictionary I added a test for this, it would previously fail to compile. Also, using `_.values()` on a Dictionary would lose strong typing on the result, returning `any[]`. This now properly returns `T[]` in that case. --- underscore/underscore-tests.ts | 17 +++++++++++++++++ underscore/underscore.d.ts | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/underscore/underscore-tests.ts b/underscore/underscore-tests.ts index 051e000d0..24815e1ce 100644 --- a/underscore/underscore-tests.ts +++ b/underscore/underscore-tests.ts @@ -479,3 +479,20 @@ _.chain(obj).map(function (value, key) { empty[key] = value; console.log("vk", value, key); }); + +function strong_typed_values_tests() { + var dictionaryLike: { [k: string] : {title: string, value: number} } = { + 'test' : { title: 'item1', value: 5 }, + 'another' : { title: 'item2', value: 8 }, + 'third' : { title: 'item3', value: 10 } + }, + empty = {}; + + _.chain(dictionaryLike).values().filter((r) => { + return r.value >= 8; + }).map((r) => { + return [r.title, true]; + }).object().value(); + + _.values<{title: string, value: number}>(dictionaryLike); +} diff --git a/underscore/underscore.d.ts b/underscore/underscore.d.ts index 8cf98071b..5b7e6a2f3 100644 --- a/underscore/underscore.d.ts +++ b/underscore/underscore.d.ts @@ -1192,6 +1192,13 @@ interface UnderscoreStatic { **/ keys(object: any): string[]; + /** + * Return all of the values of the object's properties. + * @param object Retrieve the values of all the properties on this object. + * @return List of all the values on `object`. + **/ + values(object: _.Dictionary): T[]; + /** * Return all of the values of the object's properties. * @param object Retrieve the values of all the properties on this object. @@ -1641,6 +1648,7 @@ interface UnderscoreStatic { * @return Wrapped `obj`. **/ chain(obj: T[]): _Chain; + chain(obj: _.Dictionary): _Chain; chain(obj: T): _Chain; }