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

Bug: invoke, onDone type incompatibility when exactOptionalPropertyTypes: true #5119

Open
pauldesmondparker opened this issue Nov 7, 2024 · 2 comments
Labels

Comments

@pauldesmondparker
Copy link

pauldesmondparker commented Nov 7, 2024

XState version

XState version 5

Description

Reproduction on typescritplang.org.

typescript: v5.6.3
xstate: 5.18.2

Code is the same as the #invoke-and-typescript example with a few tweaks.

Navigate to TS Config and set exactOptionalPropertyTypes.
image

The resulting error on onDone will be:

Caution

          The types of '_out_TActor.src' are incompatible between these types.
            Type 'string' is not assignable to type '"fetchUser"'. [2322]

Note

This only happens when I'm trying to use assign, otherwise no error.

  • (Probably anything in actions will set it off, but I didn't test that).

Expected result

No typescript error.

Actual result

   typescript: Type '{ actions: ActionFunction<{ userId: number; user?: User; }, DoneActorEvent<User, string>, AnyEventObject, undefined, ProvidedActor, never, never, never, never>; }' is not assignable to type 'SingleOrArray<TransitionConfigOrTarget<{ userId: number; user?: User; }, DoneActorEvent<User, string>, AnyEventObject, { src: "fetchUser"; logic: PromiseActorLogic<User, { userId: number; }, EventObject>; id: string | undefined; }, ... 4 more ..., MetaObject>>'.
     Types of property 'actions' are incompatible.
       Type 'ActionFunction<{ userId: number; user?: User; }, DoneActorEvent<User, string>, AnyEventObject, undefined, ProvidedActor, never, never, never, never>' is not assignable to type 'Actions<{ userId: number; user?: User; }, DoneActorEvent<User, string>, AnyEventObject, undefined, { src: "fetchUser"; logic: PromiseActorLogic<User, { ...; }, EventObject>; id: string | undefined; }, never, never, never, EventObject> | undefined'.
         Type 'ActionFunction<{ userId: number; user?: User; }, DoneActorEvent<User, string>, AnyEventObject, undefined, ProvidedActor, never, never, never, never>' is not assignable to type 'ActionFunction<{ userId: number; user?: User; }, DoneActorEvent<User, string>, AnyEventObject, undefined, { src: "fetchUser"; logic: PromiseActorLogic<User, { ...; }, EventObject>; id: string | undefined; }, never, never, never, EventObject>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
           The types of '_out_TActor.src' are incompatible between these types.
             Type 'string' is not assignable to type '"fetchUser"'. [2322]

Reproduction

https://www.typescriptlang.org/play/?exactOptionalPropertyTypes=true&ts=5.6.3#code/JYWwDg9gTgLgBAbzgQwM6uAcwHYBo4BmUEIACsSMKgKb40wCuYcAvoRXAOQAeqMyMapwDccOAHpxcGAE8w1VAC44AVgB0ARgAcagEwAoQ8GyCoBZAGNqcAKo0oifWOAATZdgYgARtSjCncNjIINTKfFDGmP4shtTckLBwFhDYfHAglgAWxtYAvHD0TAAUCAGy8kqOYmLJJnEwyghsaFXVYgz2AJJugZ4+fgFtHb4A-Mp2vv5tLLgBMwGWMNCVpW0E1DAWmRNQykQk5CRU1AA8O-hIw1Dd7n2+rAB8RWgy2BZwJXDGYAzwLACUcFyD1abVqaSgCkgqTyKAA7shgPBDpQaGpIagIAAbABu1BKgzaXx631+aiu3VmROqQRCyk4AAkGJgIJwqUSAVMiZDGFBsHAMdCaFyxAD2fMAWoLJCBNQALJZHIEsSSOBqdUBWqCbgNUFwCk9AAsunF7OMSOAyCx9NcWKE7L4spWhNtoT1YhSjUJbWoLiROwAIhtEViVtJkFBMBt6ViIMg-dhMDtOKx2dM06nCbH45Edl7qcYcRAANZu1bUgpQCz09abbb2Nne6qk3WfLX1ViA4EfcsV-VdHrtnXkgf4VUAFTk1mo2AI0CsLmkEDgPgKMAiiabov+GbaKQDKTLW+qi2AKUqaAwOGVfeqV2Un2oeJMnaBIKfM5gaggvx+MDHUgAMrrikmBYjI0hTouLQ7Me267tU8zUkhiGmvonJAA

Additional context

No response

@pauldesmondparker pauldesmondparker changed the title Bug: invoke, onDone type incompatability when exactOptionalPropertyTypes: true Bug: invoke, onDone type incompatibility when exactOptionalPropertyTypes: true Nov 7, 2024
@pauldesmondparker
Copy link
Author

Short term remediation:

// @ts-expect-error exactOptionalPropertyTypes

@pauldesmondparker
Copy link
Author

pauldesmondparker commented Nov 20, 2024

EDIT:

Warning

This doesn't work for more complicated cases; i.e. an array of actions.

Second remediation:

        onDone: {
          actions: ({ event }) => assign({
            user: event.output, // Strongly typed as User
          }),
        },

Making actions an anonymous function, either returning a single action or an array of actions works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant