Fix error in icepick defs for getIn/updateIn, improve generics

This commit is contained in:
Tobias Cohen 2016-11-14 06:42:31 +11:00
parent efb5680468
commit f50b8998c6
2 changed files with 23 additions and 19 deletions

View File

@ -27,7 +27,7 @@ class Foo {}
// assoc(collection, key, value)
{
let coll = { a: 1, b: 2 };
let newColl = i.assoc(coll, "b", 3); // {a: 1, b: 3}
let newColl = i.assoc<typeof coll, number>(coll, "b", 3); // {a: 1, b: 3}
let arr = ["a", "b", "c"];
let newArr = i.assoc(arr, 2, "d"); // ["a", "b", "d"]
@ -36,7 +36,7 @@ class Foo {}
// alias: set(collection, key, value)
{
let coll = { a: 1, b: 2 };
let newColl = i.set(coll, "b", 3); // {a: 1, b: 3}
let newColl = i.set<typeof coll, number>(coll, "b", 3); // {a: 1, b: 3}
let arr = ["a", "b", "c"];
let newArr = i.set(arr, 2, "d"); // ["a", "b", "d"]
@ -70,7 +70,7 @@ class Foo {}
}
};
let newColl = i.assocIn(coll, ["c", "d"], "baz");
let newColl = i.assocIn<typeof coll, string>(coll, ["c", "d"], "baz");
let coll2 = {};
let newColl2 = i.assocIn(coll2, ["a", "b", "c"], 1);
@ -86,7 +86,7 @@ class Foo {}
}
};
let newColl = i.setIn(coll, ["c", "d"], "baz");
let newColl = i.setIn<typeof coll, string>(coll, ["c", "d"], "baz");
let coll2 = {};
let newColl2 = i.setIn(coll2, ["a", "b", "c"], 1);
@ -99,7 +99,7 @@ class Foo {}
{ b: 2 }
]);
let result = i.getIn(coll, [1, "b"]); // 2
let result = i.getIn(coll, [1, "b"]) as number; // 2
}
// updateIn(collection, path, callback)
@ -166,9 +166,13 @@ class Foo {}
};
let result = i.chain(o)
.assocIn(["a", 2], 4)
.assocIn<number>(["a", 2], 4)
.setIn<number>(["a", 1], 5)
.updateIn<number>(["d"], function(d) { return d * 2 })
.merge({ b: { c: 2, c2: 3 } })
.assoc("e", 2)
.assoc<number>("e", 2)
.set<number>("f", 3)
.dissoc("d")
.value();
.getIn(['a', 0])
.value() as number;
}

22
icepick/index.d.ts vendored
View File

@ -1,15 +1,15 @@
// Type definitions for icepick v1.1.0
// Type definitions for icepick v1.3.0
// Project: https://github.com/aearly/icepick
// Definitions by: Nathan Brown <https://github.com/ngbrown>
// Definitions by: Nathan Brown <https://github.com/ngbrown>, Tobias Cohen <https://github.com/tobico>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export declare function freeze<T>(collection: T): T;
export declare function thaw<T>(collection: T): T;
export declare function assoc<T>(collection: T, key: number | string, value: any): T;
export declare function assoc<T, V>(collection: T, key: number | string, value: V): T;
export declare function dissoc<T>(collection: T, key: number | string): T;
export declare function assocIn<T>(collection: T, path: Array<number | string>, value: any): T;
export declare function getIn<Result>(collection: any, path: Array<number | string>): Result;
export declare function assocIn<T, V>(collection: T, path: Array<number | string>, value: V): T;
export declare function getIn<T>(collection: T, path: Array<number | string>): any;
export declare function updateIn<T, V>(collection: T, path: Array<number | string>, callback: (value: V) => V): T;
export {assoc as set};
@ -44,17 +44,17 @@ interface IcepickWrapper<T> {
freeze(): IcepickWrapper<T>;
thaw(): IcepickWrapper<T>;
assoc(key: number | string, value: any): IcepickWrapper<T>;
set(key: number | string, value: any): IcepickWrapper<T>;
assoc<V>(key: number | string, value: V): IcepickWrapper<T>;
set<V>(key: number | string, value: V): IcepickWrapper<T>;
dissoc(key: number | string): IcepickWrapper<T>;
unset(key: number | string): IcepickWrapper<T>;
assocIn(path: Array<number | string>, value: any): IcepickWrapper<T>;
setIn(path: Array<number | string>, value: any): IcepickWrapper<T>;
assocIn<V>(path: Array<number | string>, value: V): IcepickWrapper<T>;
setIn<V>(path: Array<number | string>, value: V): IcepickWrapper<T>;
getIn<Result>(collection: any, path: Array<number | string>): IcepickWrapper<Result>;
updateIn<T, V>(collection: T, path: Array<number | string>, callback: (value: V) => V): IcepickWrapper<T>;
getIn(path: Array<number | string>): IcepickWrapper<any>;
updateIn<V>(path: Array<number | string>, callback: (value: V) => V): IcepickWrapper<T>;
assign<S1>(source1: S1): IcepickWrapper<T & S1>;
assign<S1, S2>(s1: S1, s2: S2): IcepickWrapper<T & S1 & S2>;