Skip to content

Commit 3b05bd1

Browse files
fix assoc typing, suggested in @whitecolor's #90 and and #119
1 parent 48d2d24 commit 3b05bd1

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

index.d.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,30 @@ declare namespace R {
382382
/**
383383
* Makes a shallow clone of an object, setting or overriding the specified property with the given value.
384384
*/
385-
assoc<T,U>(prop: Prop, val: T, obj: U): {prop: T} & U;
386-
assoc<T>(prop: Prop, val: T): <U>(obj: U) => {prop: T} & U;
387-
assoc<T,U>(prop: Prop): CurriedFn2<T,U, {prop: T} & U>;
388-
// assoc<T,U>: CurriedFn3<Prop, T, U, {prop: T} & U>;
385+
386+
// extend object with new property
387+
assoc<T, U extends Struct<any>, K extends keyof U>(prop: K, val: T, obj: U): {[P in K]: T} & U;
388+
assoc<T, U extends Struct<any>, K extends keyof U>(prop: K, val: T): (obj: U) => {[P in K]: T} & U; // generics too early?
389+
assoc<T, U extends Struct<any>, K extends keyof U>(prop: K): CurriedFn2<T,U, {[P in K]: T} & U>; // generics too early?
390+
// assoc<T, U extends Struct<any>, K extends keyof U>: CurriedFn3<K, T, U, {[P in K]: T} & U>;
391+
392+
// // homogeneous object
393+
// assoc<T, U extends Struct<T>>(prop: Prop, val: T, obj: U): U;
394+
// assoc<T>(prop: Prop, val: T): <U extends Struct<T>>(obj: U) => U;
395+
// assoc<T, U extends Struct<T>>(prop: Prop): CurriedFn2<T, U, U>; // generics too early?
396+
// // assoc<T, U extends Struct<T>>: CurriedFn3<Prop, T, U, U>;
397+
398+
// any object as long as the type remains unchanged
399+
assoc<T>(prop: Prop, val: any, obj: T): T;
400+
assoc(prop: Prop, val: any): <T>(obj: T) => T;
401+
assoc<T>(prop: Prop): CurriedFn2<any, T, T>; // generics too early?
402+
// assoc<T>: CurriedFn3<Prop, any, T, T>;
403+
404+
// // broken alternative trying to be dynamic, seems not yet possible in current TS versions
405+
// assoc<T,U>(prop: Prop, val: T, obj: U): {prop: T} & U;
406+
// assoc<T>(prop: Prop, val: T): <U>(obj: U) => {prop: T} & U;
407+
// assoc<T,U>(prop: Prop): CurriedFn2<T,U, {prop: T} & U>;
408+
// // assoc<T,U>: CurriedFn3<Prop, T, U, {prop: T} & U>;
389409

390410

391411
/**

test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,16 +1266,16 @@ class Rectangle {
12661266
y: number;
12671267
}
12681268
// var xLens = R.lens(R.prop('x'), R.assoc('x'));
1269-
var xLens = R.lens<number, xy>(R.prop('x'), R.assoc('x'));
1270-
// var xLens = R.lens<number>(R.prop('x'))(R.assoc('x'));
1269+
// var xLens = R.lens<number, xy>(R.prop('x'), R.assoc('x'));
1270+
var xLens = R.lens<number>(R.prop('x'))(R.assoc('x'));
12711271
// ^ works with only 1 generic, for curried version managed to split the inferred generic from the manual generic
1272-
R.view(xLens, {x: 1, y: 2}); //=> 1
1273-
R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
1274-
R.set(xLens)(4, {x: 1, y: 2}); //=> {x: 4, y: 2}
1275-
R.set(xLens, 4)({x: 1, y: 2}); //=> {x: 4, y: 2}
1276-
R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
1277-
R.over(xLens, R.negate)({x: 1, y: 2}); //=> {x: -1, y: 2}
1278-
R.over(xLens)(R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
1272+
let r1: number = R.view(xLens, {x: 1, y: 2}); //=> 1
1273+
let r2: xy = R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
1274+
let r3: xy = R.set(xLens)(4, {x: 1, y: 2}); //=> {x: 4, y: 2}
1275+
let r4: xy = R.set(xLens, 4)({x: 1, y: 2}); //=> {x: 4, y: 2}
1276+
let r5: xy = R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
1277+
let r6: xy = R.over(xLens, R.negate)({x: 1, y: 2}); //=> {x: -1, y: 2}
1278+
let r7: xy = R.over(xLens)(R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
12791279
}
12801280
() => {
12811281
var headLens = R.lensIndex(0);

0 commit comments

Comments
 (0)