Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using guard with Refinement #157

Open
JalilArfaoui opened this issue Nov 21, 2022 · 2 comments
Open

Allow using guard with Refinement #157

JalilArfaoui opened this issue Nov 21, 2022 · 2 comments

Comments

@JalilArfaoui
Copy link

IMHO this should work

interface A {
  a: string;
}
interface B extends A {
  b: string;
}
declare const aIsB: Refinement<A, B>;
declare const a: A;
declare const doSomethingWithB : (b: B) => string;

guard<A, string>([[aIsB, doSomethingWithB]])(constant(''))(a);

Right now I have to :

guard<A, string>([[aIsB, (a) => doSomethingWithB(a as B)]])(constant(''))(a);

to make TypeScript happy …

@samhh
Copy link
Owner

samhh commented Nov 21, 2022

I suspect we'd need to rewrite the function to be overloaded so that each branch can infer its own generics, but there might be a smarter way with newer type system features.

Taking a step back, have you considered using Options instead? Here's an alternative approach with altAllBy and function composition:

const guardA: (x: A) => Option<string> = altAllBy([
  flow(getB, O.map(doSomethingWithB)),
  flow(getB, O.map(doSomethingElseWithB)),
  constant(O.of('foo')),
])

@JalilArfaoui
Copy link
Author

Thanks @samhh

I still prefer my workaround as I don't need an Option as result …

But thanks for altAllBy that I didn't knew …

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants