selfrefactor / rambdax

Extended version of Rambda
https://selfrefactor.github.io/rambdax
MIT License
221 stars 26 forks source link

refactor: improve types #83

Closed bfaulk96 closed 1 year ago

bfaulk96 commented 2 years ago

I looked into a way we could improve dotwalking types on unknown objects! You can see below how these types work in practice. It's worth noting this relies on Typescript 4.1+

Screen Shot 2022-06-14 at 2 01 01 PM
selfrefactor commented 2 years ago

Thanks for this MR - it looks really nice. I will try to move forward with next Rambdax version, but it won't happen this month. I will keep you informed as soon as there is a progress.

bfaulk96 commented 2 years ago

Thanks for this MR - it looks really nice. I will try to move forward with next Rambdax version, but it won't happen this month. I will keep you informed as soon as there is a progress.

@selfrefactor Awesome, thanks! It's also worth noting – I noticed this with applyDiff, but I assume this type could also be applied to many other functions to give better type safety/completions 😄

selfrefactor commented 1 year ago

I will close the MR as I fail to see the successful test case.

I receive this error:

Argument of type '({ op: string; path: string; value?: undefined; } | { op: string; path: string; value: number; })[]' is not assignable to parameter of type 'ApplyDiffRule<"a.c" | "a.b" | "a">[]'. Type '{ op: string; path: string; value?: undefined; } | { op: string; path: string; value: number; }' is not assignable to type 'ApplyDiffRule<"a.c" | "a.b" | "a">'. Type '{ op: string; path: string; value?: undefined; }' is not assignable to type 'ApplyDiffRule<"a.c" | "a.b" | "a">'. Type '{ op: string; path: string; value?: undefined; }' is not assignable to type 'ApplyDiffAdd<"a.c" | "a.b" | "a">'. Types of property 'op' are incompatible. Type 'string' is not assignable to type '"add"'.

for this test:

 const obj = {a: {b: 1, c: 2}}
    const rules = [
      {op: 'remove', path: 'a.c'},
      {op: 'add', path: 'a.d', value: 4},
      {op: 'update', path: 'a.b', value: 2},
    ]
    const result = applyDiff(rules, obj)

If you do manage to provide a passing test, then I will be happy to review it.