无法从TypeScript项目引用的全局声明中获取实际类型

huangapple go评论74阅读模式
英文:

Unable to Get Real Type from Global Declaration in TypeScript Project References

问题

我已经创建了一个演示存储库来说明我的问题。该存储库包含两个项目:electronsrc,我正在使用TypeScript项目引用。

我在src/global.d.ts文件中通过从electron/src/index.ts文件中导入它来定义了API_TYPE类型。然而,Visual Studio Code(VSCode)无法从src/global.d.ts正确地推断出API_TYPE的实际类型。当我在VSCode中打开src/main.ts文件时,我看到一个工具提示表明该类型未被识别。
无法从TypeScript项目引用的全局声明中获取实际类型

我怀疑这个问题可能与tsconfig.json文件中的references字段有关。如果我移除references字段,一切都正常工作。然而,我不能移除references字段,因为electronsrc项目需要保持分开。

以下是src/global.d.ts中的相关代码:

import type { API_TYPE } from "../electron/src/index";

declare global {
  interface Window {
    electronAPI: PRELOAD_API_TYPE<API_TYPE>;
  }
}

export {};

我将非常感谢您对如何解决这个问题的任何见解或建议。谢谢!


2023-05-30更新

我已经重新上传了演示存储库。之前的演示正常运行,无法重现我的问题。

此外,我已经确定问题是由Vue组件引起的。删除Vue组件src/components/HelloWorld.vue可以正确推断出window.electronAPI类型。


2023-05-31更新

我已经重新创建了一个在线演示,但令我惊讶的是,它实际上正常工作。然而,当我将此项目下载到我的本地计算机并使用本地的VSCode编辑器打开时,类型推断结果未正确显示。具体来说,当我将鼠标悬停在src/main.ts文件中的apiAPI_TYPE变量上时,本地的VSCode不会显示它们对应的类型。

下面的截图显示了在线演示显示正确类型的情况。
无法从TypeScript项目引用的全局声明中获取实际类型

下面的截图说明了本地VSCode编辑器无法显示正确类型的问题。
无法从TypeScript项目引用的全局声明中获取实际类型

英文:

I have created a demo repository to illustrate my issue. The repository contains two projects: electron and src, and I am using TypeScript project references.

I have defined the type API_TYPE in the src/global.d.ts file by importing it from the electron/src/index.ts file. However, Visual Studio Code (VSCode) is unable to correctly infer the real type of API_TYPE from src/global.d.ts. When I open the src/main.ts file in VSCode, I see a tooltip indicating that the type is not recognized.
无法从TypeScript项目引用的全局声明中获取实际类型

I suspect the issue might be related to the references field in the tsconfig.json file. If I remove the references field, everything works fine. However, I cannot remove the references field because the electron and src projects need to be separate.

Here is the relevant code from src/global.d.ts:

import type { API_TYPE } from "../electron/src/index";

declare global {
  interface Window {
    electronAPI: PRELOAD_API_TYPE<API_TYPE>;
  }
}

export {};

I would appreciate any insights or suggestions on how to resolve this issue. Thank you!


Edit at 05-30

I have reuploaded the demo repository. The previous demo was functioning correctly and could not reproduce my issue.

Furthermore, I have identified that the problem is caused by the Vue component. Removing the Vue component src/components/HelloWorld.vue allows for correct inference of the type of window.electronAPI.


Edit at 05-31

I have re-created an online demo, but to my surprise, it actually works properly. However, when I download this project to my local machine and open it using the local VSCode editor, the type inference results are not displayed correctly. Specifically, when I hover over the api and API_TYPE variables in the src/main.ts file, the local VSCode does not show their corresponding types.

The screenshot below shows an online demo displaying the correct types.
无法从TypeScript项目引用的全局声明中获取实际类型

The screenshot below illustrates the issue where the local VSCode editor fails to display the correct types.
无法从TypeScript项目引用的全局声明中获取实际类型

答案1

得分: 0

我觉得我找到了一个解决方案。只需启用 Volar 的接管模式,应该可以正常工作。

https://cn.vuejs.org/guide/typescript/overview.html#volar-takeover-mode

英文:

I think I have found a solution. Just need to enable the takeover mode of Volar and it should work correctly.

https://cn.vuejs.org/guide/typescript/overview.html#volar-takeover-mode

huangapple
  • 本文由 发表于 2023年5月29日 16:24:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定