ts-type-guards icon indicating copy to clipboard operation
ts-type-guards copied to clipboard

feat: predicate filtering

Open flisboac opened this issue 6 years ago • 1 comments

This PR introduces a slight modification of the isArrayOf and only themes. Two new functions are introduced as more generic alternatives to the primitive and classy type guards:

  • isArrayOfAll (and its readonly counterpart isReadonlyArrayOfAll) is the same as isArrayOf but receives type guards instead of classy/like type information.
  • onlyIf is the same as only, but receives type guards instead of classy/like type information, just like isArrayOfAll.

With them, one can abstract away primitive or classy-type-specific type guards by depending solely on the TypeGuard concept.

This feature plays (or at least I expect that it will play) rather well with guard chaining; instead of having mono-typed array checks, e.g. isArrayOfStrings(x) || isArrayOfNumbers(x), one can check for arrays of diverse types, with the added possibility of single evaluation of the operand and a single curried type guard, e.g. isArrayOfAll(guard(isString).or(isNumber))(x) (typed as x is string | number).

In the future, I will submit a new PR that does interface checking by means of an interface descriptor, and the predicate filtering functionality will play a vital role in enabling a more flexible filtering in cases where a classy type is not available at runtime (e.g. when using only interfaces).

One PR remaining! :D

flisboac avatar Apr 15 '19 01:04 flisboac

@SimonAlling , I still think there's merit in keeping this PR, so I'm keeping it. If you accept it, and if there's anything I must fix or improve, please do tell me!

flisboac avatar Jul 11 '19 07:07 flisboac