首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xstate类型与TState的不兼容

Xstate类型与TState的不兼容
EN

Stack Overflow用户
提问于 2022-09-06 13:53:17
回答 1查看 16关注 0票数 0

我已经开始使用xstate/fsm,声明TState类型也有问题。

代码语言:javascript
复制
import {createMachine, EventObject, StateMachine} from '@xstate/fsm';
import {Typestate} from "@xstate/fsm/lib/types";
export interface InitContext {
  retries: number;
}
export interface InitState extends Typestate<InitContext> {
  value: any;
  context: InitContext;
}
export type InitEvent =
    | { type: "LOG_IN" }
    | { type: "FETCHED" }
    | { type: "ERROR" }
    | { type: "RESTART" };
export type InitEvent =
| { type: "LOG_IN" }
| { type: "FETCHED" }
| { type: "ERROR" }
| { type: "RESTART" }

machine!: StateMachine.Machine<InitContext, InitEvent, InitState>;
machine2!: StateMachine.Machine<InitContext, EventObject, { value: any; context: InitContext }>;

this.machine = createMachine<InitContext>({}); // this does not work
this.machine2 = createMachine<InitContext>({}); // this works

错误是:

代码语言:javascript
复制
TS2322: Type 'Machine<InitContext, EventObject, { value: any; context: InitContext; }>' is not assignable to type 'Machine<InitContext, InitEvent, InitState>'.   
Types of property 'transition' are incompatible.     
Type '(state: string | State<InitContext, EventObject, { value: any; context: InitContext; }>, event: string | EventObject) => State<...>' is not assignable to type '(state: string | State<InitContext, InitEvent, InitState>, event: "FETCH_CONFIG" | "LOG_IN" | "FETCH_MAIN_DATA" | "FETCH_LINKED_DATA" | "FETCHED" | "ERROR" | "RESTART" | InitEvent) => State<...>'.       
Types of parameters 'state' and 'state' are incompatible.         
Type 'string | State<InitContext, InitEvent, InitState>' is not assignable to type 'string | State<InitContext, EventObject, { value: any; context: InitContext; }>'.           
Type 'State<InitContext, InitEvent, InitState>' is not assignable to type 'string | State<InitContext, EventObject, { value: any; context: InitContext; }>'.             
Type 'State<InitContext, InitEvent, InitState>' is not assignable to type 'State<InitContext, EventObject, { value: any; context: InitContext; }>'.
Types of property 'actions' are incompatible.                 
Type 'ActionObject<InitContext, InitEvent>[]' is not assignable to type 'ActionObject<InitContext, EventObject>[]'.
Type 'ActionObject<InitContext, InitEvent>' is not assignable to type 'ActionObject<InitContext, EventObject>'.
Type 'EventObject' is not assignable to type 'InitEvent'.

如何正确定义InitStateInitEvent?方法createMachine被定义为:

代码语言:javascript
复制
export declare function createMachine<TContext extends object, TEvent extends EventObject = EventObject, TState extends Typestate<TContext> = {
    value: any;
    context: TContext;
}>(fsmConfig: StateMachine.Config<TContext, TEvent, TState>, implementations?: {
    actions?: StateMachine.ActionMap<TContext, TEvent>;
}): StateMachine.Machine<TContext, TEvent, TState>;
EN

回答 1

Stack Overflow用户

发布于 2022-09-08 10:06:19

这条路:

代码语言:javascript
复制
this.machine = createMachine<InitContext, InitEvent, InitState>({
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73623238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档