XState typed Actions conflicting with Event types

I’m using XState(5.18.2) with TS. Some event types require extra parameters, some don’t. Actions use the event types, but don’t seem to differentiate which events are used by which action.

Simple reproduction here:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { setup } from 'xstate'
const exampleMachine = setup({
types: {
events: {} as {
| { type: 'eventWithNoParams' }
| { type: 'eventWithParams', meta: string }
}
},
actions: {
doSomething({context, event}, params) {
event.meta // typeError: Property 'meta' does not exist on type { type: 'eventWithNoParams' } | { type: 'eventWithParams', meta: string }
params // is type unknown
}
}
}).createMachine({
id: 'example',
initial: 'start',
states: {
start: {
on: {
'eventWithNoParams': 'nextState'
}
},
nextState: {
on: {
'eventWithParams': 'nextState'
actions: 'doSomething'
}
},
finalState: {
type: 'final'
}
}
})
const actor = createActor(exampleMachine)
actor.start()
actor.send({type: 'eventWithNoParams'}) // state -> nextState
actor.send({type: 'eventWithParams', meta: 'someData' }) // state -> finalState
</code>
<code>import { setup } from 'xstate' const exampleMachine = setup({ types: { events: {} as { | { type: 'eventWithNoParams' } | { type: 'eventWithParams', meta: string } } }, actions: { doSomething({context, event}, params) { event.meta // typeError: Property 'meta' does not exist on type { type: 'eventWithNoParams' } | { type: 'eventWithParams', meta: string } params // is type unknown } } }).createMachine({ id: 'example', initial: 'start', states: { start: { on: { 'eventWithNoParams': 'nextState' } }, nextState: { on: { 'eventWithParams': 'nextState' actions: 'doSomething' } }, finalState: { type: 'final' } } }) const actor = createActor(exampleMachine) actor.start() actor.send({type: 'eventWithNoParams'}) // state -> nextState actor.send({type: 'eventWithParams', meta: 'someData' }) // state -> finalState </code>
import { setup } from 'xstate'

const exampleMachine = setup({
  types: {
    events: {} as {
      | { type: 'eventWithNoParams' }
      | { type: 'eventWithParams', meta: string }
    }
  },
  actions: {
    doSomething({context, event}, params) {
      event.meta // typeError: Property 'meta' does not exist on type { type: 'eventWithNoParams' } | { type: 'eventWithParams', meta: string }
      params // is type unknown
    }
  }
}).createMachine({
  id: 'example',
  initial: 'start',
  states: {
    start: {
      on: {
        'eventWithNoParams': 'nextState'
      }
    },
    nextState: {
      on: {
        'eventWithParams': 'nextState'
        actions: 'doSomething'
      }
    },
    finalState: {
      type: 'final'
    }
  }
})

const actor = createActor(exampleMachine)
actor.start()
actor.send({type: 'eventWithNoParams'}) // state -> nextState
actor.send({type: 'eventWithParams', meta: 'someData' }) // state -> finalState

The code is working fine, and the actions are operating correctly, but I’m getting errors in the action definitions and get zero type safety. I don’t know how to ensure only certain event types are used by certain actions. I can’t find any examples in the documentation with typed examples of both multiple events with extra parameters as well as multiple actions.

Relevant links:

  • https://stately.ai/docs/setup
  • https://stately.ai/docs/transitions
  • https://stately.ai/docs/typescript

A similar question was asked here: XState: Types of property ‘actions’ are incompatible but is in v4 which has a completely different type system and syntax.

After a bit more reading and some help from the discord I’ve figured it out. The correct syntax is:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { setup } from 'xstate'
const exampleMachine = setup({
types: {
events: {} as {
| { type: 'eventWithNoParams' }
| { type: 'eventWithParams', meta: string }
}
},
actions: {
doSomething({context, event}, params: { meta: string }) {
params.meta // simply retype param
}
}
}).createMachine({
id: 'example',
initial: 'start',
states: {
start: {
on: {
'eventWithNoParams': 'nextState'
}
},
nextState: {
on: {
'eventWithParams': 'nextState'
actions: {
type: 'doSomething'
params: ({ event }) => ({ meta: event.meta }) // pass param here from event. You'll get type inference in event here from the types up top and type inference from the action param typing.
}
}
},
finalState: {
type: 'final'
}
}
})
const actor = createActor(exampleMachine)
actor.start()
actor.send({type: 'eventWithNoParams'}) // state -> nextState
actor.send({type: 'eventWithParams', meta: 'someData' }) // state -> finalState
</code>
<code>import { setup } from 'xstate' const exampleMachine = setup({ types: { events: {} as { | { type: 'eventWithNoParams' } | { type: 'eventWithParams', meta: string } } }, actions: { doSomething({context, event}, params: { meta: string }) { params.meta // simply retype param } } }).createMachine({ id: 'example', initial: 'start', states: { start: { on: { 'eventWithNoParams': 'nextState' } }, nextState: { on: { 'eventWithParams': 'nextState' actions: { type: 'doSomething' params: ({ event }) => ({ meta: event.meta }) // pass param here from event. You'll get type inference in event here from the types up top and type inference from the action param typing. } } }, finalState: { type: 'final' } } }) const actor = createActor(exampleMachine) actor.start() actor.send({type: 'eventWithNoParams'}) // state -> nextState actor.send({type: 'eventWithParams', meta: 'someData' }) // state -> finalState </code>
import { setup } from 'xstate'

const exampleMachine = setup({
  types: {
    events: {} as {
      | { type: 'eventWithNoParams' }
      | { type: 'eventWithParams', meta: string }
    }
  },
  actions: {
    doSomething({context, event}, params: { meta: string }) {
      params.meta // simply retype param
    }
  }
}).createMachine({
  id: 'example',
  initial: 'start',
  states: {
    start: {
      on: {
        'eventWithNoParams': 'nextState'
      }
    },
    nextState: {
      on: {
        'eventWithParams': 'nextState'
        actions: {
          type: 'doSomething'
          params: ({ event }) => ({ meta: event.meta }) // pass param here from event. You'll get type inference in event here from the types up top and type inference from the action param typing.
        }
      }
    },
    finalState: {
      type: 'final'
    }
  }
})

const actor = createActor(exampleMachine)
actor.start()
actor.send({type: 'eventWithNoParams'}) // state -> nextState
actor.send({type: 'eventWithParams', meta: 'someData' }) // state -> finalState

A little confusing that I had to type it twice but I think it makes sense given any event could potentially trigger any action. For larger payloads the type could be extracted as a type or interface for better reusability.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>...
interface Payload { meta: string }
...
types: {
events: {} as {
| { type: 'eventWithNoParams' }
| ({ type: 'eventWithParams' } & Payload)
...
doSomething({context, event}, params: Payload)
...
</code>
<code>... interface Payload { meta: string } ... types: { events: {} as { | { type: 'eventWithNoParams' } | ({ type: 'eventWithParams' } & Payload) ... doSomething({context, event}, params: Payload) ... </code>
...
interface Payload { meta: string }
...
types: {
    events: {} as {
      | { type: 'eventWithNoParams' }
      | ({ type: 'eventWithParams' } & Payload)
...
    doSomething({context, event}, params: Payload)
...

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật