網站搜尋

typescript - Pinia useStore 回傳 any


我正在使用 Vue3 和 TypeScript 的 Pinia。在使用多種不同方式對 store 進行類型定義時,我無法將 store 定義為任何其他類型,除了 any。

以下是實作:

import { defineStore } from "pinia";
import type { DataT } from "@/types";

interface DataStore {
  data: DataT[];
}

export const useDataStore = defineStore("dataStore", {
  state: (): DataStore => ({
    data: [] as DataT[],
  }),
});

無論這個商店在組件中被引入到哪裡,useDataStore() 的返回類型都是 any,這不是類型安全的。我如何才能從我的商店獲得類型安全?

所有 Vue 元件都使用 <script lang="ts" setup>,並且在其他地方成功地使用了其他 TS 類型。

這裡是一個它如何被導入的範例:

<script lang="ts" setup>
// some other imports
import { useDataStore } from "../../store/data";

const dataStore = useDataStore();
// other code follows
</script>