Neovim中TypeScript类型提示泛滥,新手求助配置方案
解决方案
一、提供类型建议/自动补全的组件
在LazyVim中,负责TypeScript/JavaScript类型提示与自动补全的核心组件是:
- tsserver:微软官方的TypeScript语言服务器,提供类型检查、定义跳转、行内类型提示等核心功能,通过
nvim-lspconfig完成配置。 - nvim-cmp:Neovim的自动补全框架,配合
cmp-nvim-lsp插件,将tsserver输出的类型提示转化为可选择的补全项展示。
二、关闭.js文件中的TypeScript类型定义提示
你可以通过配置tsserver的inlayHints选项,精准关闭JavaScript文件里的各类行内类型提示,操作步骤如下:
- 在LazyVim配置目录(通常为
~/.config/nvim/lua/plugins/)下,创建或修改lsp.lua文件,添加以下配置:
return { "neovim/nvim-lspconfig", opts = { servers = { tsserver = { settings = { javascript = { inlayHints = { -- 关闭JS文件中所有类型提示项 includeInlayEnumMemberValueHints = false, includeInlayFunctionLikeReturnTypeHints = false, includeInlayFunctionParameterTypeHints = false, includeInlayParameterNameHints = "none", includeInlayParameterNameHintsWhenArgumentMatchesName = false, includeInlayPropertyDeclarationTypeHints = false, includeInlayVariableTypeHints = false, }, }, }, }, }, }, }
- 保存配置后,重启Neovim或执行
:Lazy reload nvim-lspconfig使配置生效。
可选方案:完全禁用.js文件的tsserver(不推荐)
如果希望tsserver完全不处理JavaScript文件,可修改tsserver的filetypes配置,排除JavaScript相关类型:
return { "neovim/nvim-lspconfig", opts = { servers = { tsserver = { filetypes = { "typescript", "typescriptreact", "typescript.tsx" }, }, }, }, }
注意:此方法会让.js文件失去tsserver提供的所有功能,如需保留JS语言支持,需额外配置其他JavaScript语言服务器(如vscode-langservers-extracted中的javascript-language-server)。
三、调整console.log时的信息面板
输入console.log()时弹出的面板是LSP的签名帮助(signature help),可通过以下配置控制其触发行为:
-- 在你的配置文件中添加 vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with( vim.lsp.handlers.signatureHelp, { -- 禁用自动触发,仅在手动调用时显示(可绑定快捷键如<leader>sh) triggerCharacters = {}, } )
内容的提问来源于stack exchange,提问作者Taylor Johnson
相关产品推荐
相关产品推荐

