TypeScript Language Server
Language Server Protocol implementation for TypeScript wrapping tsserver
.
Based on concepts and ideas from https://github.com/prabirshrestha/typescript-language-server and originally maintained by TypeFox
Maintained by a community of contributors like you
Installing
npm install -g typescript-language-server typescript
Running the language server
typescript-language-server --stdio
CLI Options
Usage: typescript-language-server [options]
Options:
-V, --version output the version number
--stdio use stdio (required option)
--log-level <log-level> A number indicating the log level (4 = log, 3 = info, 2 = warn, 1 = error). Defaults to `3`.
--tsserver-log-verbosity <verbosity> [deprecated] Specify tsserver log verbosity (off, terse, normal, verbose). Defaults to `normal`. example: --tsserver-log-verbosity=verbose
--tsserver-path <path> [deprecated] Specify path to tsserver directory. example: --tsserver-path=/Users/me/typescript/lib/
-h, --help output usage information
The --tsserver-log-verbosity
and --tsserver-path
options are deprecated and it is recommended to pass those through corresponding tsserver.*
initializationOptions
instead.
Note: The path passed to --tsserver-path
should be a path to the [...]/typescript/lib/tssserver.js
file or to the [...]/typescript/lib/
directory and not to the shell script [...]/node_modules/.bin/tsserver
. Though for backward-compatibility reasons, the server will try to do the right thing even when passed a path to the shell script.
initializationOptions
The language server accepts various settings through the initializationOptions
object passed through the initialize
request. Refer to your LSP client's documentation on how to set these. Here is the list of supported options:
Setting | Type | Description |
---|
hostInfo | string | Information about the host, for example "Emacs 24.4" or "Sublime Text v3075" . Default: undefined |
completionDisableFilterText | boolean | Don't set filterText property on completion items. Default: false |
disableAutomaticTypingAcquisition | boolean | Disables tsserver from automatically fetching missing type definitions (@types packages) for external modules. |
maxTsServerMemory | number | The maximum size of the V8's old memory section in megabytes (for example 4096 means 4GB). The default value is dynamically configured by Node so can differ per system. Increase for very big projects that exceed allowed memory usage. Default: undefined |
npmLocation | string | Specifies the path to the NPM executable used for Automatic Type Acquisition. |
locale | string | The locale to use to show error messages. |
plugins | object[] | An array of { name: string, location: string } objects for registering a Typescript plugins. Default: [] |
preferences | object | Preferences passed to the Typescript (tsserver ) process. See below for more |
tsserver | object | Options related to the tsserver process. See below for more |
The tsserver
setting specifies additional options related to the internal tsserver
process, like tracing and logging.
interface TsserverOptions {
logDirectory?: string;
logVerbosity?: 'off' | 'terse' | 'normal' | 'requestTime' | 'verbose';
path?: string;
trace?: 'off' | 'messages' | 'verbose';
useSyntaxServer?: 'auto' | 'never';
}
The preferences
object is an object specifying preferences for the internal tsserver
process. Those options depend on the version of Typescript used but at the time of writing Typescript v4.4.3 contains these options:
interface UserPreferences {
autoImportFileExcludePatterns: [],
disableSuggestions: boolean;
quotePreference: "auto" | "double" | "single";
includeCompletionsForModuleExports: boolean;
includeCompletionsForImportStatements: boolean;
includeCompletionsWithSnippetText: boolean;
includeCompletionsWithInsertText: boolean;
includeAutomaticOptionalChainCompletions: boolean;
includeCompletionsWithClassMemberSnippets: boolean;
includeCompletionsWithObjectLiteralMethodSnippets: boolean;
useLabelDetailsInCompletionEntries: boolean;
allowIncompleteCompletions: boolean;
importModuleSpecifierPreference: "shortest" | "project-relative" | "relative" | "non-relative";
importModuleSpecifierEnding: "auto" | "minimal" | "index" | "js";
allowTextChangesInNewFiles: boolean;
lazyConfiguredProjectsFromExternalProject: boolean;
organizeImportsIgnoreCase: "auto" | boolean;
organizeImportsCollation: "ordinal" | "unicode";
organizeImportsCollationLocale: string;
organizeImportsNumericCollation: boolean;
organizeImportsAccentCollation: boolean;
organizeImportsCaseFirst: "upper" | "lower" | false;
providePrefixAndSuffixTextForRename: boolean;
provideRefactorNotApplicableReason: boolean;
allowRenameOfImportPath: boolean;
includePackageJsonAutoImports: "auto" | "on" | "off";
jsxAttributeCompletionStyle: "auto" | "braces" | "none";
displayPartsForJSDoc: boolean;
generateReturnInDocTemplate: boolean;
includeInlayParameterNameHints: "none" | "literals" | "all";
includeInlayParameterNameHintsWhenArgumentMatchesName: boolean;
includeInlayFunctionParameterTypeHints: boolean,
includeInlayVariableTypeHints: boolean;
includeInlayVariableTypeHintsWhenTypeMatchesName: boolean;
includeInlayPropertyDeclarationTypeHints: boolean;
includeInlayFunctionLikeReturnTypeHints: boolean;
includeInlayEnumMemberValueHints: boolean;
}
From the preferences
options listed above, this server explicilty sets the following options (all other options use their default values):
{
allowIncompleteCompletions: true,
allowRenameOfImportPath: true,
allowTextChangesInNewFiles: true,
displayPartsForJSDoc: true,
generateReturnInDocTemplate: true,
includeAutomaticOptionalChainCompletions: true,
includeCompletionsForImportStatements: true,
includeCompletionsForModuleExports: true,
includeCompletionsWithClassMemberSnippets: true,
includeCompletionsWithObjectLiteralMethodSnippets: true,
includeCompletionsWithInsertText: true,
includeCompletionsWithSnippetText: true,
jsxAttributeCompletionStyle: "auto",
providePrefixAndSuffixTextForRename: true,
provideRefactorNotApplicableReason: true,
}
workspace/didChangeConfiguration
Some of the preferences can be controlled through the workspace/didChangeConfiguration
notification. Below is a list of supported options that can be passed. Note that the settings are specified separately for the typescript and javascript files so [language]
can be either javascript
or typescript
.
[language].format.baseIndentSize: number;
[language].format.convertTabsToSpaces: boolean;
[language].format.indentSize: number;
[language].format.indentStyle: 'None' | 'Block' | 'Smart';
[language].format.insertSpaceAfterCommaDelimiter: boolean;
[language].format.insertSpaceAfterConstructor: boolean;
[language].format.insertSpaceAfterFunctionKeywordForAnonymousFunctions: boolean;
[language].format.insertSpaceAfterKeywordsInControlFlowStatements: boolean;
[language].format.insertSpaceAfterOpeningAndBeforeClosingEmptyBraces: boolean;
[language].format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: boolean;
[language].format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: boolean;
[language].format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: boolean;
[language].format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: boolean;
[language].format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: boolean;
[language].format.insertSpaceAfterSemicolonInForStatements: boolean;
[language].format.insertSpaceAfterTypeAssertion: boolean;
[language].format.insertSpaceBeforeAndAfterBinaryOperators: boolean;
[language].format.insertSpaceBeforeFunctionParenthesis: boolean;
[language].format.insertSpaceBeforeTypeAnnotation: boolean;
[language].format.newLineCharacter: string;
[language].format.placeOpenBraceOnNewLineForControlBlocks: boolean;
[language].format.placeOpenBraceOnNewLineForFunctions: boolean;
[language].format.semicolons: 'ignore' | 'insert' | 'remove';
[language].format.tabSize: number;
[language].format.trimTrailingWhitespace: boolean;
[language].inlayHints.includeInlayEnumMemberValueHints: boolean;
[language].inlayHints.includeInlayFunctionLikeReturnTypeHints: boolean;
[language].inlayHints.includeInlayFunctionParameterTypeHints: boolean;
[language].inlayHints.includeInlayParameterNameHints: 'none' | 'literals' | 'all';
[language].inlayHints.includeInlayParameterNameHintsWhenArgumentMatchesName: boolean;
[language].inlayHints.includeInlayPropertyDeclarationTypeHints: boolean;
[language].inlayHints.includeInlayVariableTypeHints: boolean;
[language].inlayHints.includeInlayVariableTypeHintsWhenTypeMatchesName: boolean;
completions.completeFunctionCalls: boolean;
diagnostics.ignoredCodes: number[];
implicitProjectConfiguration.checkJs: boolean;
implicitProjectConfiguration.experimentalDecorators: boolean;
implicitProjectConfiguration.module: string;
implicitProjectConfiguration.strictFunctionTypes: boolean;
implicitProjectConfiguration.strictNullChecks: boolean;
implicitProjectConfiguration.target: string;
Code actions on save
Server announces support for the following code action kinds:
source.fixAll.ts
- despite the name, fixes a couple of specific issues: unreachable code, await in non-async functions, incorrectly implemented interfacesource.removeUnused.ts
- removes declared but unused variablessource.addMissingImports.ts
- adds imports for used but not imported symbolssource.removeUnusedImports.ts
- removes unused importssource.sortImports.ts
- sorts importssource.organizeImports.ts
- organizes and removes unused imports
This allows editors that support running code actions on save to automatically run fixes associated with those kinds.
Those code actions, if they apply in the current code, should also be presented in the list of "Source Actions" if the editor exposes those.
The user can enable it with a setting similar to (can vary per-editor):
"codeActionsOnSave": {
"source.organizeImports.ts": true,
"source.organizeImports": true,
}
Workspace commands (workspace/executeCommand
)
See LSP specification.
Most of the time, you'll execute commands with arguments retrieved from another request like textDocument/codeAction
. There are some use cases for calling them manually.
lsp
refers to the language server protocol types, tsp
refers to the typescript server protocol types.
Go to Source Definition
(This command is supported from Typescript 4.7.)
Apply Workspace Edits
Apply Code Action
Apply Refactoring
Organize Imports
Rename File
Configure plugin
Inlay hints (textDocument/inlayHint
)
For the request to return any results, some or all of the following options need to be enabled through preferences
:
export interface InlayHintsOptions extends UserPreferences {
includeInlayParameterNameHints: 'none' | 'literals' | 'all';
includeInlayParameterNameHintsWhenArgumentMatchesName: boolean;
includeInlayFunctionParameterTypeHints: boolean;
includeInlayVariableTypeHints: boolean;
includeInlayVariableTypeHintsWhenTypeMatchesName: boolean;
includeInlayPropertyDeclarationTypeHints: boolean;
includeInlayFunctionLikeReturnTypeHints: boolean;
includeInlayEnumMemberValueHints: boolean;
}
TypeScript Version Notification
Right after initializing, the server sends a custom $/typescriptVersion
notification that carries information about the version of TypeScript that is utilized by the server. The editor can then display that information in the UI.
The $/typescriptVersion
notification params include two properties:
version
- a semantic version (for example 4.8.4
)source
- a string specifying whether used TypeScript version comes from the local workspace (workspace
), is explicitly specified through a initializationOptions.tsserver.path
setting (user-setting
) or was bundled with the server (bundled
)
Supported Protocol features
Development
Build
yarn
Test
yarn test
- run all testsyarn test:watch
- run all tests and enable watch mode for developing
By default only console logs of level warning
and higher are printed to the console. You can override the CONSOLE_LOG_LEVEL
level in package.json
to either log
, info
, warning
or error
to log other levels.
Watch
yarn watch
Publishing
New version of the package is published automatically on pushing new tag to the upstream repo.