rambdax icon indicating copy to clipboard operation
rambdax copied to clipboard

refactor: improve types

Open bfaulk96 opened this issue 3 years ago • 2 comments
trafficstars

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

bfaulk96 avatar Jun 14 '22 18:06 bfaulk96

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 avatar Jun 15 '22 05:06 selfrefactor

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 😄

bfaulk96 avatar Jun 15 '22 13:06 bfaulk96

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.

selfrefactor avatar Dec 03 '22 19:12 selfrefactor