0

所以似乎出于某种原因,不确定是谁的错 - 打字稿类型没有被识别......

例如,我使用“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>;

在这段代码中:

  1. “useAccountStore”的类型正确 在此处输入图像描述
  2. “StoreDefinition”类型是一个返回“Store”作为返回类型的函数: 在此处输入图像描述
  3. VS Code 认为它返回一个“任何”类型:

在此处输入图像描述

  1. 甚至,当我明确地将其转换为“Store”类型时,它会导致“any” 在此处输入图像描述
  • VS 代码版本:1.64.1
  • VS Code 打字稿版本:4.5.5

任何想法可能会发生什么?

  • 这似乎不是包本身的问题,因为我在互联网上以及 YouTube 上的教程上都没有发现任何类似的问题一定是我的 Typescript 配置/版本有问题吗?
  • 我确实将我的 Visual Studio Code 升级到了最新版本
  • 在 Visual Studio Code 开发工具中 - 我看不到与打字稿相关的警告或错误
4

1 回答 1

1

Storepinia 的 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那么整个事情就解决了。

于 2022-02-18T19:37:46.703 回答