From 3b05bd1041567bee2bc2604c03a93098f9be6662 Mon Sep 17 00:00:00 2001 From: tycho01 Date: Sun, 11 Dec 2016 02:29:30 +0800 Subject: [PATCH] fix assoc typing, suggested in @whitecolor's #90 and and #119 --- index.d.ts | 28 ++++++++++++++++++++++++---- test.ts | 18 +++++++++--------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/index.d.ts b/index.d.ts index 2b65a1b..6a0802a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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(prop: Prop, val: T, obj: U): {prop: T} & U; - assoc(prop: Prop, val: T): (obj: U) => {prop: T} & U; - assoc(prop: Prop): CurriedFn2; - // assoc: CurriedFn3; + + // extend object with new property + assoc, K extends keyof U>(prop: K, val: T, obj: U): {[P in K]: T} & U; + assoc, K extends keyof U>(prop: K, val: T): (obj: U) => {[P in K]: T} & U; // generics too early? + assoc, K extends keyof U>(prop: K): CurriedFn2; // generics too early? + // assoc, K extends keyof U>: CurriedFn3; + + // // homogeneous object + // assoc>(prop: Prop, val: T, obj: U): U; + // assoc(prop: Prop, val: T): >(obj: U) => U; + // assoc>(prop: Prop): CurriedFn2; // generics too early? + // // assoc>: CurriedFn3; + + // any object as long as the type remains unchanged + assoc(prop: Prop, val: any, obj: T): T; + assoc(prop: Prop, val: any): (obj: T) => T; + assoc(prop: Prop): CurriedFn2; // generics too early? + // assoc: CurriedFn3; + + // // broken alternative trying to be dynamic, seems not yet possible in current TS versions + // assoc(prop: Prop, val: T, obj: U): {prop: T} & U; + // assoc(prop: Prop, val: T): (obj: U) => {prop: T} & U; + // assoc(prop: Prop): CurriedFn2; + // // assoc: CurriedFn3; /** diff --git a/test.ts b/test.ts index c7fdfb8..6fb9bf6 100644 --- a/test.ts +++ b/test.ts @@ -1266,16 +1266,16 @@ class Rectangle { y: number; } // var xLens = R.lens(R.prop('x'), R.assoc('x')); - var xLens = R.lens(R.prop('x'), R.assoc('x')); - // var xLens = R.lens(R.prop('x'))(R.assoc('x')); + // var xLens = R.lens(R.prop('x'), R.assoc('x')); + var xLens = R.lens(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);