Home

Awesome

<!-- markdownlint-configure-file { "line-length": false, "no-inline-html": false } -->

ARCHIVED

typescript.nvim is now archived and will no longer receive updates. Please see this issue for details.

typescript.nvim

A minimal typescript-language-server integration plugin to set up the language server via nvim-lspconfig and add commands for convenience. Written in TypeScript and transpiled to Lua using TypeScriptToLua.

This plugin is in beta status. It's stable enough for daily use, but breaking changes are possible.

Requires Neovim 0.7.

Setup

Install the plugin with your favorite plugin manager, then add require("typescript").setup() to your config. This will set up the plugin and typescript-language-server with default settings.

The following example shows all available options and their defaults:

require("typescript").setup({
    disable_commands = false, -- prevent the plugin from creating Vim commands
    debug = false, -- enable debug logging for commands
    go_to_source_definition = {
        fallback = true, -- fall back to standard LSP definition on failure
    },
    server = { -- pass options to lspconfig's setup method
        on_attach = ...,
    },
})

Note that command-specific configuration affects Vim commands, not the Lua API.

Important: if you have require("lspconfig").tsserver.setup({}) anywhere in your config, make sure to remove it and pass any options you were using under the server key. lspconfig doesn't allow more than one setup call, so your config will not work as expected.

Features

Commands

The plugin exposes Vim commands as well as a Lua API. Vim commands are buffer-local, so you'll have access to them once tsserver has attached.

The following commands are async by default, but you can make them run synchronously by adding a ! to Vim commands or passing { sync = true } to Lua commands.

Handlers

The plugin defines handlers for off-spec methods that are not otherwise supported by Neovim.

Integrations

null-ls

Instead of using the above Vim commands, you can instead add commands to your code action menu using null-ls.

Commands Available as Code Actions

Code Action Setup

require("null_ls").setup({
  sources = {
    ..., -- add to your other sources
    require("typescript.extensions.null-ls.code-actions"),
  },
})

Will not support

FAQ

require("typescript").setup({
  server = {
    on_attach = function(client, bufnr)
      -- your other on_attach stuff here if you have any
      -- ...
      vim.lsp.buf.inlay_hint(bufnr, true)
    end,
    settings={
      -- specify some or all of the following settings if you want to adjust the default behavior
      javascript = {
        inlayHints = {
          includeInlayEnumMemberValueHints = true,
          includeInlayFunctionLikeReturnTypeHints = true,
          includeInlayFunctionParameterTypeHints = true,
          includeInlayParameterNameHints = "all", -- 'none' | 'literals' | 'all';
          includeInlayParameterNameHintsWhenArgumentMatchesName = true,
          includeInlayPropertyDeclarationTypeHints = true,
          includeInlayVariableTypeHints = true,
        },
      },
      typescript = {
        inlayHints = {
          includeInlayEnumMemberValueHints = true,
          includeInlayFunctionLikeReturnTypeHints = true,
          includeInlayFunctionParameterTypeHints = true,
          includeInlayParameterNameHints = "all", -- 'none' | 'literals' | 'all';
          includeInlayParameterNameHintsWhenArgumentMatchesName = true,
          includeInlayPropertyDeclarationTypeHints = true,
          includeInlayVariableTypeHints = true,
        },
      },
    },
  },
})

Contributing

TypeScript Code

  1. Clone the repo and run npm install.
  2. Change or add TypeScript source files under the src/ directory.
  3. Try out your changes locally using npm run dev.
  4. Build your changes before committing using npm run build.

Lua Code

Lua-only extensions live in the extensions/ directory. Running npm run build copies extensions to the appropriate plugin directory and makes them available to Neovim under the typescript.extensions namespace (or a subdirectory if you choose to use one).

If you update or add a Lua file, make sure to run npm run build before committing! Your changes will not take effect otherwise.

Tests

Integration tests are in Lua and depend on plenary.nvim. Run make test from the root of the repo.