[英]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
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:
所以当我把事情改成这样时:
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.最后一点是显示的大小是有限的(可能是出于性能原因),所以即使在显示类型定义的情况下,如果它太长,它有时也会简单地截断它。
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.