kenkunz / svelte-fsm

Tiny, expressive finite state machines for svelte
MIT License
275 stars 9 forks source link

Cannot Call debounce on this keyword in TypeScript #12

Open amorfati254 opened 1 year ago

amorfati254 commented 1 year ago

const progress = tweened(0, { easing: cubicOut });
    const state = fsm('loaded', {
        loaded: {
            load: 'preloading'
        },
        preloading: {
            _enter() {
                progress.set(0, { duration: 0 });
                this.advance.debounce(250);
            },
            advance: 'loading',
            complete: 'loaded'
        },
        loading: {
            _enter() {
                progress.set(80, { duration: 5000 });
            },
            _exit() {
                progress.set(100, { duration: 1000 });
            },
            complete: 'loaded'
        }
    });

this.advance.debounce shows TS error debounce does not exist on type string

cristianvogel commented 9 months ago

I can confirm the same thing. Debounce in the context recommended by documentation is throwing a type error, because this.* is always returning a string.

Its probably related to https://github.com/kenkunz/svelte-fsm/issues/6

Also, I looked at the source code and conclude that debounce is late binding?

By making a Typescript type assertion, I was able to satisfy the ts check.


interface SetWithDebounce {
    debounce: (delay: number) => void;
} 

.....

 updatingHost: {
            _enter() {
                SourceOfChange.set('ui');
                (this.set as unknown as SetWithDebounce).debounce(1)
            },
            set: 'ready'
        }