Skip to content

Commit

Permalink
fix assoc typing, suggested in @whitecolor's #90 and and #119
Browse files Browse the repository at this point in the history
  • Loading branch information
KiaraGrouwstra committed Dec 10, 2016
1 parent 48d2d24 commit 3b05bd1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
28 changes: 24 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,30 @@ declare namespace R {
/**
* Makes a shallow clone of an object, setting or overriding the specified property with the given value.
*/
assoc<T,U>(prop: Prop, val: T, obj: U): {prop: T} & U;
assoc<T>(prop: Prop, val: T): <U>(obj: U) => {prop: T} & U;
assoc<T,U>(prop: Prop): CurriedFn2<T,U, {prop: T} & U>;
// assoc<T,U>: CurriedFn3<Prop, T, U, {prop: T} & U>;

// extend object with new property
assoc<T, U extends Struct<any>, K extends keyof U>(prop: K, val: T, obj: U): {[P in K]: T} & U;
assoc<T, U extends Struct<any>, K extends keyof U>(prop: K, val: T): (obj: U) => {[P in K]: T} & U; // generics too early?
assoc<T, U extends Struct<any>, K extends keyof U>(prop: K): CurriedFn2<T,U, {[P in K]: T} & U>; // generics too early?
// assoc<T, U extends Struct<any>, K extends keyof U>: CurriedFn3<K, T, U, {[P in K]: T} & U>;

// // homogeneous object
// assoc<T, U extends Struct<T>>(prop: Prop, val: T, obj: U): U;
// assoc<T>(prop: Prop, val: T): <U extends Struct<T>>(obj: U) => U;
// assoc<T, U extends Struct<T>>(prop: Prop): CurriedFn2<T, U, U>; // generics too early?
// // assoc<T, U extends Struct<T>>: CurriedFn3<Prop, T, U, U>;

// any object as long as the type remains unchanged
assoc<T>(prop: Prop, val: any, obj: T): T;
assoc(prop: Prop, val: any): <T>(obj: T) => T;
assoc<T>(prop: Prop): CurriedFn2<any, T, T>; // generics too early?
// assoc<T>: CurriedFn3<Prop, any, T, T>;

// // broken alternative trying to be dynamic, seems not yet possible in current TS versions
// assoc<T,U>(prop: Prop, val: T, obj: U): {prop: T} & U;
// assoc<T>(prop: Prop, val: T): <U>(obj: U) => {prop: T} & U;
// assoc<T,U>(prop: Prop): CurriedFn2<T,U, {prop: T} & U>;
// // assoc<T,U>: CurriedFn3<Prop, T, U, {prop: T} & U>;


/**
Expand Down
18 changes: 9 additions & 9 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1266,16 +1266,16 @@ class Rectangle {
y: number;
}
// var xLens = R.lens(R.prop('x'), R.assoc('x'));
var xLens = R.lens<number, xy>(R.prop('x'), R.assoc('x'));
// var xLens = R.lens<number>(R.prop('x'))(R.assoc('x'));
// var xLens = R.lens<number, xy>(R.prop('x'), R.assoc('x'));
var xLens = R.lens<number>(R.prop('x'))(R.assoc('x'));
// ^ works with only 1 generic, for curried version managed to split the inferred generic from the manual generic
R.view(xLens, {x: 1, y: 2}); //=> 1
R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
R.set(xLens)(4, {x: 1, y: 2}); //=> {x: 4, y: 2}
R.set(xLens, 4)({x: 1, y: 2}); //=> {x: 4, y: 2}
R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
R.over(xLens, R.negate)({x: 1, y: 2}); //=> {x: -1, y: 2}
R.over(xLens)(R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
let r1: number = R.view(xLens, {x: 1, y: 2}); //=> 1
let r2: xy = R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
let r3: xy = R.set(xLens)(4, {x: 1, y: 2}); //=> {x: 4, y: 2}
let r4: xy = R.set(xLens, 4)({x: 1, y: 2}); //=> {x: 4, y: 2}
let r5: xy = R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
let r6: xy = R.over(xLens, R.negate)({x: 1, y: 2}); //=> {x: -1, y: 2}
let r7: xy = R.over(xLens)(R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
}
() => {
var headLens = R.lensIndex(0);
Expand Down

0 comments on commit 3b05bd1

Please sign in to comment.