ramda: add generic typing to Evolver (#41672)

* Add generic typing to Evolver

* Remove partial import

* Fix lint issue
This commit is contained in:
Stuart Long
2020-01-22 09:54:48 -08:00
committed by Ben Lichtman
parent e88aced7df
commit 88eddeabe2
2 changed files with 33 additions and 4 deletions

View File

@@ -58,4 +58,31 @@ import * as R from 'ramda';
});
const ex3Test: { a: { b: string; c: null; d: { e: string } } } = ex3;
// Evolver with a generic:
const ev1: R.Evolver<{ a: string }> = { a: R.always("b") };
// Evolver supports partial transformation:
const ev2: R.Evolver<{ a: string, b: boolean }> = { b: (b: boolean) => true };
// Evolver disallows unknown prop:
// $ExpectError
const ev3: R.Evolver<{ a: string }> = { b: R.not };
// Typed Evolver disallows nesting:
const ev4: R.Evolver<{ a: { b: boolean } }> = { a: { b: R.not } };
// Evolver disallows nesting for primitives:
// $ExpectError
const ev5: R.Evolver<{ a: string }> = { a: { b: R.not } };
// Evolver needs function:
// $ExpectError
const ev6: R.Evolver = { a: 1 };
};

View File

@@ -174,11 +174,13 @@ type Evolved<A> =
: never;
/**
* <needs description>
* A set of transformation to run as part of an evolve
* @param T - the type to be evolved
*/
export interface Evolver {
[key: string]: ((value: any) => any) | Evolver;
}
export type Evolver<T extends Evolvable<any> = any> = {
// if T[K] isn't evolvable, don't allow nesting for that property
[key in keyof Partial<T>]: ((value: T[key]) => T[key]) | (T[key] extends Evolvable<any> ? Evolver<T[key]> : never);
};
/**
* <needs description>