spectacles-ts icon indicating copy to clipboard operation
spectacles-ts copied to clipboard

Type error when traversing Record with fixed keys

Open fabianpage opened this issue 2 years ago • 1 comments

🐛 Bug report

Current Behavior

Hi Thanks for this awesome library! I think the type of the Path to traverse a Record with fixed Keys is wrong.

  type A = {
    b: Record<string, C>;
  };

  type AWithFixedKeys = {
    b: Record<"c", C>;
  };

  type C = {
    d: string;
  };

  const anA: A = { b: { someKey: { d: "test" } } };
  const anAWithKeys: AWithFixedKeys = { b: { c: { d: "test" } } };

  const setA = pipe(anA, set("b.{}>.d", "someValue"));
  const setAKeys = pipe(anAWithKeys, set("b.{}>.d", "someOtherValue"));

  const getA = pipe(setA, get("b.{}>.d"));
  const getAKeys = pipe(setAKeys, get("b.{}>.d"));

  console.log({ getA }, { getAKeys });

The return value is okay:

 { getA: [ 'someValue' ] } { getAKeys: [ 'someOtherValue' ] }

But on the 2 paths i get the following error:

src/entries.ts:117:42 - error TS2345: Argument of type '"b.{}>.d"' is not assignable to parameter of type '"" | "b" | "b.c" | "b.c.d"'.

117   const setAKeys = pipe(anAWithKeys, set("b.{}>.d", "someOtherValue"));
                                             ~~~~~~~~~

src/entries.ts:120:39 - error TS2345: Argument of type '"b.{}>.d"' is not assignable to parameter of type '"" | "b" | "b.c" | "b.c.d"'.

120   const getAKeys = pipe(setAKeys, get("b.{}>.d"));
                                          ~~~~~~~~~

Expected behavior

Not have an type error :)

Your environment

Which versions of fp-ts are affected by this issue? Did this work in previous versions of fp-ts?

Software Version(s)
spectacles-ts 1.0.7
fp-ts 2.12.11
TypeScript 4.6.3

fabianpage avatar May 30 '22 07:05 fabianpage

I created an failing test at https://github.com/tegonal/spectacles-ts/tree/recordWithKeys

Later i will try to have a look into Paths.ts and see if i can fix this type error.

fabianpage avatar May 30 '22 07:05 fabianpage