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

Weird type error with nested objects with index type #23

Open
G-Rath opened this issue Apr 5, 2019 · 4 comments
Open

Weird type error with nested objects with index type #23

G-Rath opened this issue Apr 5, 2019 · 4 comments

Comments

@G-Rath
Copy link
Contributor

G-Rath commented Apr 5, 2019

I'm not too sure why this error is happening, and was hoping you could shed some light on if this is a bug with this library, or something I just have to work around:

interface Mx {
  data: {
    prop: string | null;

    [K: string]: unknown;
  };
}

const f = makeFactory<Mx>({ data: { prop: null } });

const f1 = f.build();
const f2 = f.build({
  ...f1,
  data: {
    ...f1.data,
    v: 'hello world'
  }
});

The above code gives the following error:

TS2345: Argument of type '{ data: { v: string; prop: string | null; }; }' is not assignable to parameter of type 'RecPartial<Mx>'.
  Types of property 'data' are incompatible.
    Type '{ v: string; prop: string | null; }' is not assignable to type 'RecPartial<{ [K: string]: unknown; prop: string | null; }>'.
      Property 'prop' is incompatible with index signature.
        Type 'string | null' is not assignable to type 'RecPartial<unknown> | undefined'.
          Type 'null' is not assignable to type 'RecPartial<unknown> | undefined'.

If I change the index type to be any, v becomes the problem:

TS2345: Argument of type '{ data: { v: string; prop: string | null; }; }' is not assignable to parameter of type 'RecPartial<Mx>'.
  Types of property 'data' are incompatible.
    Type '{ v: string; prop: string | null; }' is not assignable to type 'RecPartial<{ [K: string]: any; prop: string | null; }>'.
      Property 'v' is incompatible with index signature.
        Type 'string' is not assignable to type 'RecPartial<any> | undefined'.

The error goes away when I do the following:

  • Remove the ...f1.data
  • Add prop: <string> (but not prop: null)
  • Add prop: undefined
  • Change the signature of prop to string | undefined

None of these work if I use any instead of unknown, nor if I use a subfactory:

const f2 = f.build({
  ...f1,
  data: makeFactory<Mx['data']>({ prop: null }).build({
    ...f1.data,
    v: 'string'
  })
});

Overall, I'm pretty confused by this 😂
Let me know if there's anything else I can provide!

@sashashakun
Copy link

I see similar error

@willryan
Copy link
Owner

@G-Rath @sashashakun What version of Typescript, and what version of factory.ts are you using? I can't seem to repro. I don't see any compile errors with the example code. Thanks.

@G-Rath
Copy link
Contributor Author

G-Rath commented May 25, 2019

@willryan Sorry I thought I'd included a minimal repo, but guess I didn't.

I can repo it w/ the above code + npm i -D typescript factory.ts.

@G-Rath
Copy link
Contributor Author

G-Rath commented Jun 17, 2019

Here are the versions:

"factory.ts": "^0.5.0",
"typescript": "^3.4.2"

(This is still a problem in typescript@3.5.2)

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

3 participants