Skip to content

@Includes / @Excludes

State guard decorators that restrict method calls to specific states.

@Includes

function Includes(...states: string[]): MethodDecorator

Only allows the call when the current state is in states, otherwise throws FSMError.

class Player extends FSM {
@Includes('playing')
pause() {
// only callable in the playing state
}
}

Error message:

{className} {action} failed: current state {state} not in {states}

this.state.toString() is used for comparison (so intermediate states’ toString() participates).

@Excludes

function Excludes(...states: string[]): MethodDecorator

Disallows the call when the current state is in states.

class Player extends FSM {
@Excludes('disabled')
play() {
// callable in any state except disabled
}
}

Multiple states

@Includes('playing', 'buffering')
pause() {}
@Excludes('disabled', 'errored')
play() {}

Combining with @ChangeState

Decorators stack, applied bottom-up:

@Includes('idle') // outer: validate first
@ChangeState('idle', 'done') // inner: then transition
async fetch() {}

Usually you’ll use them alone — @ChangeState already includes from validation.

See also