selfrefactor / rambda

Faster and smaller alternative to Ramda
https://selfrefactor.github.io/rambda
MIT License
1.65k stars 89 forks source link

`R.anyPass()` can function as a type guard #695

Closed Peeja closed 1 year ago

Peeja commented 1 year ago

In the specific case where R.anyPass() is combining unary type guards, it can now serve as a unary type guard itself. This allows you to do things like:

const isJsonLiteral = anyPass([isString, isNumber, isBoolean, isNull]);

I would have liked to do allPass as well for completeness, but the TypeScript is much more complex, and I'm not sure it comes up as much.

selfrefactor commented 1 year ago

I see the use case and I see it as interesting TS code. Just before merging, would you explain how this works input is U[number] as I cannot understand what U[number] means in this context?

Peeja commented 1 year ago

Sure thing! It's a little odd, to be sure. Here, U is an array type, indexed by number keys, so U[number] means the type of an element of that array. U[0] would be the type of the first element; U[number] is the type of some element, without knowing which one, which makes it a union of all of the element types (even when it's a tuple and each element can have a different type).

U[keyof U] may work just as well, now that I think of it, but I feel like I tried that and it ran into trouble for some reason.

selfrefactor commented 1 year ago

Thanks for the info. I will merge it as it is, but I will try U[keyof U] as if it works will be easier for others to understand. As with your other MR, I will inform you once NPM version is published.

Peeja commented 1 year ago

Thanks! I agree, that reads much better, if it works.

selfrefactor commented 1 year ago

closing as release was made and feature is now part of the code