简体   繁体   English

如何在 VSCode、TypeScript 中查看 hover 的完整类型定义?

[英]How to view full type definition on hover in VSCode, TypeScript?

Hovering over a variable or function in VSCode is really helpful by showing its type.将鼠标悬停在 VSCode 中的变量或 function 上通过显示其类型非常有用。 However, there are certain use-cases where I would like to see the full type definition of a variable and not just the type alias.但是,在某些用例中,我希望查看变量的完整类型定义,而不仅仅是类型别名。

For example: UseState with full type例如:具有完整类型的 UseState 具有完整类型的 UseState will allow me to see the full type of the variable campaign:将允许我查看变量活动的完整类型:

悬停全高清 . .

But I would really like to extrapolate that type definition and export it so other components can use it too.但我真的很想推断该类型定义并将其导出,以便其他组件也可以使用它。 So when I change things to this:所以当我把事情改成这样时:UseState 类型为别名和悬停 and hover over the variable it only shows the type alias.和 hover 在变量上它只显示类型别名。 How can I get TypeScript or VSCode to emit the full type definition for a type alias?如何让 TypeScript 或 VSCode 发出类型别名的完整类型定义? Thanks!谢谢!

Unfortunately, I don't think what you want is possible.不幸的是,我不认为你想要什么是可能的。

The hover function of VSCode is only meant to give you a quick type information about the value, not the full type. VSCode 的 hover function 仅旨在为您提供有关该值的快速类型信息,而不是完整类型。 In fact, I believe this feature is powered by TypeScript directly so VSCode has no control over what is displayed...事实上,我相信这个功能是由 TypeScript 直接驱动的,所以 VSCode 无法控制显示的内容......

One final note is that the size of what is displayed is limited (probably for performance reason) so even in case where the type definition is displayed, it sometime simply truncate it if it's too long.最后一点是显示的大小是有限的(可能是出于性能原因),所以即使在显示类型定义的情况下,如果它太长,它有时也会简单地截断它。

Right click and click go to Type Definition should always take you to the typescript type.右键单击并单击go to Type Definition应始终将您带到 typescript 类型。

在此处输入图像描述

Test this:测试这个:

const [campaigns,setCampigns]= useState<{ [K in keyof campaigns]: campaigns[K]}>()

Not optimal but you can do so anyway.不是最佳的,但无论如何你都可以这样做。

You can try the option Peek Definition Alt + F12 maybe could be helpful in this case.您可以尝试选项 Peek Definition Alt + F12在这种情况下可能会有所帮助。

Go to keyboard shortcuts and add a shortcut to "Add Peek Definition". Go 到键盘快捷方式并添加快捷方式到“添加查看定义”。 This works for Typescript, and Golang struct.这适用于 Typescript 和 Golang 结构。 In my case I am using alt+command+P.就我而言,我使用的是 alt+command+P。

I will highlight the variable and press the keyboard shortcuts, and the type definition shows up like in screenshot.我将突出显示变量并按下键盘快捷键,类型定义会像屏幕截图一样显示。

在此处输入图像描述

To get type definitions, you need to set shortcut to Peek type definition command.要获取类型定义,您需要设置Peek 类型定义命令的快捷方式。 By default, it has no value.默认情况下,它没有任何价值。

First, you need to go to Keyboard shortcuts with Ctrl+k Ctrl+s or via UI.首先,您需要使用Ctrl+k Ctrl+s或通过 UI 将 go 设置为键盘快捷键

在此处输入图像描述

Then type in the search bar Peek Type Definition .然后在搜索栏中输入Peek Type Definition You'll see a corresponding command.您将看到相应的命令。

Double click on it and press preferred shortcut.双击它并按首选快捷方式。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在 VSCode(或其他地方)中显示打字稿类型/接口的完整计算类型 - Is it possible to display the full computed type of a typescript type/interface in VSCode (or elsewhere) Vscode Typescript:显示类型的别名,而不是其定义 - Vscode Typescript: Display alias of type rather than its definition 如何理解打字稿中类型定义中的`[0, 1, 2, ...0[]]`? - How to understand `[0, 1, 2, ...0[]]` in type definition in typescript? 打字稿:具有相同项目类型的字符串联合和完整数组的 DRY 定义 - Typescript: DRY definition of string union and full array with the same item type hover 中的 VSCode Javascript/Typescript 命名空间 - VSCode Javascript/Typescript namespaces in hover 如何覆盖 TypeScript 类型别名的 VSCode 工具提示? - How to override VSCode tooltip for TypeScript type alias? 如何在 JavaScript/VSCode/TypeScript 中声明 setter 的类型? - How to declare the type of a setter in JavaScript/VSCode/TypeScript? 如何在打字稿中导入类型定义? - How to import the type-definition in typescript? 如何使用打字稿文件导入/导出类型定义 - How to import/export a type definition with a typescript file 如何为getter / setter编写类型定义(typescript) - How to write type definition (typescript) for getter/setter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM