Closed AlCalzone closed 1 year ago
I vaguely remember you wanting a CodeSandbox for this but I can't find that comment now. Is that still the case?
Yes please!
Mhh, I tried but it doesn't seem to work: https://codesandbox.io/s/nervous-panini-1tz0b
src/types.ts
I'll have to close this until there is a reproducible example. Perhaps you can make a more minimal example? I will reopen when that is available.
I refactored the state machine recently and reached a much smaller repro. However I still can't get it to reproduce in the code sandbox or even my IDE. The error only appears when compiling from the CLI. If I remember correctly, this has something to do with different source file order in the language server vs the CLI.
If you can do with a reproducible example outside of the sandbox, then I recommend this:
npm install
lerna run build
- there should be no errorpackages/zwave-js/src/lib/driver/CommandQueueMachine.ts
, go to line 127 (raise("trigger") as any,
), remove the as any
lerna run build
- you'll get the following error:
src/lib/driver/CommandQueueMachine.ts(105,2): error TS2322: Type 'StateMachine<CommandQueueContext, any, CommandQueueStateSchema, { value: any; context: CommandQueueContext; }>' is not assignable to type 'CommandQueueMachine'.
The types of 'config.initial' are incompatible between these types.
Type 'string | number | symbol | undefined' is not assignable to type '"idle" | "execute" | "abortSendData" | "executeDone" | undefined'.
Type 'string' is not assignable to type '"idle" | "execute" | "abortSendData" | "executeDone" | undefined'.
src/lib/driver/CommandQueueMachine.ts(118,4): error TS2322: Type '{ add: { actions: (AssignAction<CommandQueueContext, { type: "add"; transaction: Transaction; }> | RaiseAction<EventObject> | SendAction<unknown, AnyEventObject, EventObject>)[]; }; message: { actions: SendAction<any, any, any>; }; }' is not assignable to type 'TransitionsConfigMap<CommandQueueContext, CommandQueueEvent> | TransitionsConfigArray<CommandQueueContext, CommandQueueEvent> | undefined'.
Types of property 'add' are incompatible.
Type '{ actions: (AssignAction<CommandQueueContext, { type: "add"; transaction: Transaction; }> | RaiseAction<EventObject> | SendAction<unknown, AnyEventObject, EventObject>)[]; }' is not assignable to type 'SingleOrArray<string | StateNode<CommandQueueContext, any, { type: "add"; transaction: Transaction; }, { value: any; context: CommandQueueContext; }> | TransitionConfig<CommandQueueContext, { type: "add"; transaction: Transaction; }> | undefined>'.
Types of property 'actions' are incompatible.
Type '(AssignAction<CommandQueueContext, { type: "add"; transaction: Transaction; }> | RaiseAction<EventObject> | SendAction<unknown, AnyEventObject, EventObject>)[]' is not assignable to type 'string | ActionObject<CommandQueueContext, { type: "add"; transaction: Transaction; }> | ActionFunction<CommandQueueContext, { type: "add"; transaction: Transaction; }> | Action<CommandQueueContext, { type: "add"; transaction: Transaction; }>[] | undefined'.
Type '(AssignAction<CommandQueueContext, { type: "add"; transaction: Transaction; }> | RaiseAction<EventObject> | SendAction<unknown, AnyEventObject, EventObject>)[]' is not assignable to type 'Action<CommandQueueContext, { type: "add"; transaction: Transaction; }>[]'.
Type 'AssignAction<CommandQueueContext, { type: "add"; transaction: Transaction; }> | RaiseAction<EventObject> | SendAction<unknown, AnyEventObject, EventObject>' is not assignable to type 'Action<CommandQueueContext, { type: "add"; transaction: Transaction; }>'.
Type 'SendAction<unknown, AnyEventObject, EventObject>' is not assignable to type 'Action<CommandQueueContext, { type: "add"; transaction: Transaction; }>'.
Type 'SendAction<unknown, AnyEventObject, EventObject>' is not assignable to type 'string'.
@AlCalzone Ah I see, inference isn't perfect here. The correct typing should be:
raise("trigger") as RaiseAction<CommandQueueEvent>
Looking at better ways to resolve inference for these (cc. @Andarist)
Yep, I was dealing with this issue in one of the xstate tutorials.
The raise("SKIP") as RaiseAction<{ type: "SKIP" }>
did work, but, unfortunately, it shows as an inline function in the stately editor (which is a different bug tracked here).
This may be addressed by https://github.com/statelyai/xstate/pull/3694
Description I was asked to post this here (original comment) After I turned on
strictFunctionTypes
in my repo, TypeScript started complaining aboutraise
actions in my transition definitions. In https://github.com/AlCalzone/node-zwave-js/blob/e10cb2c6d706bfc9dda0b5039ec38c651a2fbbd7/packages/zwave-js/src/lib/driver/SendThreadMachine.ts, you'll see I had toany
some of theraise
actions, e.g. line 394 or 405 (relevant commit)Expected Result Since
raise
actions don't convey any context, I wouldn't expect them to cause incompatible types.Actual Result If I remove the
as any
in line 405, this is the output fromlerna run build
:Note that some errors (including this one) are not shown in the editor or when building with
--watch
.Reproduction See linked repo
Additional context