所以似乎出于某種原因,不確定是誰的錯 - 打字稿型別沒有被識別......
例如,我使用“pinia@next”npm 包,這是一個簡單的演示代碼:
(這是檔案夾中唯一的檔案以及安裝的單個包npm install pinia@next)
import { defineStore, Store } from "pinia";
export type State = {
anyProperty: string
};
export const useAccountStore = defineStore("account", {
state: () => ({} as State),
});
var test = useAccountStore() as Store<"account", State>;
在這段代碼中:
- “useAccountStore”的型別正確

- “StoreDefinition”型別是一個回傳“Store”作為回傳型別的函式:

- VS Code 認為它回傳一個“任何”型別:

- 甚至,當我明確地將其轉換為“Store”型別時,它會導致“any”

- VS 代碼版本:1.64.1
- VS Code 打字稿版本:4.5.5
任何想法可能會發生什么?
- 這似乎不是包本身的問題,因為我在互聯網上以及 YouTube 上的教程上都沒有發現任何類似的問題一定是我的 Typescript 配置/版本有問題嗎?
- 我確實將我的 Visual Studio Code 升級到了最新版本
- 在 Visual Studio Code 開發工具中 - 我看不到與打字稿相關的警告或錯誤
uj5u.com熱心網友回復:
Store在pinia 的 types.js 檔案中定義為
export type Store<
Id extends string = string,
S extends StateTree = {},
G /* extends GettersTree<S>*/ = {},
// has the actions without the context (this) for typings
A /* extends ActionsTree */ = {}
> = _StoreWithState<Id, S, G, A> &
UnwrapRef<S> &
_StoreWithGetters<G> &
// StoreWithActions<A> &
(_ActionsTree extends A ? {} : A) &
PiniaCustomProperties<Id, S, G, A> &
PiniaCustomStateProperties<S>
如果其中任何一個解決了,any那么整個事情就解決了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/428579.html
