英文:
Unable to Get Real Type from Global Declaration in TypeScript Project References
问题
我已经创建了一个演示存储库来说明我的问题。该存储库包含两个项目:electron
和src
,我正在使用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
文件时,我看到一个工具提示表明该类型未被识别。
我怀疑这个问题可能与tsconfig.json
文件中的references
字段有关。如果我移除references
字段,一切都正常工作。然而,我不能移除references
字段,因为electron
和src
项目需要保持分开。
以下是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
文件中的api
和API_TYPE
变量上时,本地的VSCode不会显示它们对应的类型。
下面的截图说明了本地VSCode编辑器无法显示正确类型的问题。
英文:
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.
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.
The screenshot below illustrates the issue where the local VSCode editor fails to display the correct types.
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论