From 0413f0673380290daf334cc69c7c85077bfe4808 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 27 Jun 2021 22:45:19 +0200 Subject: [PATCH] test(dts): add cross call test --- test-dts/actions.test-d.ts | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 test-dts/actions.test-d.ts diff --git a/test-dts/actions.test-d.ts b/test-dts/actions.test-d.ts new file mode 100644 index 00000000..d23d720c --- /dev/null +++ b/test-dts/actions.test-d.ts @@ -0,0 +1,51 @@ +import { defineStore, expectType } from '.' + +const useStore = defineStore({ + id: 'name', + state: () => ({ count: 0 }), + actions: { + useOtherAction() { + return this.returnStuff() + }, + useExternalFunction() { + return outer(this as any) + }, + returnStuff() { + this.useOtherAction() + return this.count * 2 + }, + // return type is necessary + factorial(n?: number): number { + n ??= this.count + return n > 1 ? this.factorial(n - 1) * n : 1 + }, + crossA(): 'A' | 'B' { + if (this.count < 3) { + return 'A' + } else { + return this.crossB() + } + }, + crossB() { + if (this.count > 2) { + return this.crossA() + } else { + return 'B' + } + }, + }, +}) + +function outer(store: ReturnType): number { + return store.count * 2 +} + +const store = useStore() + +expectType<'A' | 'B'>(store.crossA()) +expectType<'A' | 'B'>(store.crossB()) +expectType(store.factorial()) +expectType(store.returnStuff()) +expectType(store.useExternalFunction()) +expectType(store.useOtherAction()) +expectType(outer(store)) -- 2.47.2