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

Typing error when sending to parent #570

Closed
posva opened this issue Jul 26, 2019 · 6 comments
Closed

Typing error when sending to parent #570

posva opened this issue Jul 26, 2019 · 6 comments

Comments

@posva
Copy link
Contributor

posva commented Jul 26, 2019

Bug

Description:

When using sendParent in an actions array with the function version, TypeScirpt complains about the states property of the machine not being valid. Setting the types of the parent yield the same result: sendParent<ParentContext, ParentEvent>(...)

(Bug) Expected result:

No TS error

(Bug) Actual result:

yarn run v1.17.3
$ /Users/posva/xstate-ts-bug/node_modules/.bin/tsc
src/index.ts(4,3): error TS2322: Type '{ one: { on: { '': { target: string; actions: (SendAction<unknown, EventObject> | SendAction<unknown, { type: "parent"; ctx: unknown; }>)[]; }; }; }; two: {}; }' is not assignable to type 'StatesConfig<DefaultContext, any, EventObject>'.
  Property 'one' is incompatible with index signature.
    Type '{ on: { '': { target: string; actions: (SendAction<unknown, EventObject> | SendAction<unknown, { type: "parent"; ctx: unknown; }>)[]; }; }; }' is not assignable to type 'StateNodeConfig<DefaultContext, any, EventObject>'.
      Types of property 'on' are incompatible.
        Type '{ '': { target: string; actions: (SendAction<unknown, EventObject> | SendAction<unknown, { type: "parent"; ctx: unknown; }>)[]; }; }' is not assignable to type 'TransitionsConfig<DefaultContext, EventObject>'.
          Property '''' is incompatible with index signature.
            Type '{ target: string; actions: (SendAction<unknown, EventObject> | SendAction<unknown, { type: "parent"; ctx: unknown; }>)[]; }' is not assignable to type 'string | number | StateNode<DefaultContext, any, EventObject> | TransitionConfig<DefaultContext, EventObject> | TransitionConfig<DefaultContext, EventObject>[] | undefined'.
              Type '{ target: string; actions: (SendAction<unknown, EventObject> | SendAction<unknown, { type: "parent"; ctx: unknown; }>)[]; }' is not assignable to type 'undefined'.

(Bug) Potential fix:

I think the type should be a bit looser as using a simple sendParent('parent') doesn't yield any error

Link to reproduction or proof-of-concept:

I'm sorry I couldn't reproduce the TS error on a codesandbox but this is a fully boiled down repro with only typescript and xstate as deps

https://github.com/posva/xstate-ts-bug

repro steps:

  • yarn
  • yarn tsc
@Andarist
Copy link
Member

This seems similar to TS having problem inferring the types sometimes for assign, where providing generics for it fixes the problem. In the same manner changing to:

sendParent<DefaultContext, EventObject>(ctx => ({ type: 'parent', ctx })),

or

sendParent((ctx: DefaultContext, ev: EventObject) => ({ type: 'parent', ctx })),

fixes this.

Ofc this is not ideal, but not sure how much can be done here right now to fix this with current APIs. What I have learned exploring the problem with assign is that TS tries to infer based on arguments and this happens to fail in some scenarios

@davidkpiano
Copy link
Member

davidkpiano commented Jul 26, 2019

In 4.7, there will might be better typing inference that allows you to do assign(), send(), etc. without needing to pass in the context/event types. (this is difficult to do!)

@Andarist
Copy link
Member

I can't reproduce this right now (at least not inside XState repository). The context type seems to flow correctly to sendParent.

@posva
Copy link
Contributor Author

posva commented Jul 31, 2019

It shows on the sample project repository, it really is boiled down to the minimum, xstate and typescript are the only packages and this is the tsconfig:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": ["es2015"],
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "strict": true
  }
}

Maybe the strict produces the type error as it's one of the differences between both tsconfig files.

I also tried by cloning xstate and linking locally and the type error still appears.

I forgot to say that both of the explicit typings mentioned above do not affect the error and it still appears

@davidkpiano
Copy link
Member

@posva What TS version are you using? On 3.5.3, I don't get any type errors.

@posva
Copy link
Contributor Author

posva commented Aug 1, 2019

@davidkpiano Yeah, I'm really confused I don't understand how you cannot get the error on the repo I sent you. I'm using the latest node v10, packages are up to date, running yarn tsc or npx tsc shows the compiler error. Even if I clone xstate locally and link it after running npm i, I still get the error. It does happen on a bigger project too. I can also reproduce it inside the xstate project (even though we will always install a library so I think the other repro is more relevant), here you have a fork with the error showing: https://github.com/posva/xstate/tree/types-bug-report

It all comes to activating strictFunctionTypes, if that check is set to false, there is no error. I don't know if this is intentional. I haven't checked the specific rule on typescript yet and it could also be a bug upstream, in typescript

davidkpiano added a commit that referenced this issue Mar 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants