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.
npm install -g typescript-language-server typescript
typescript-language-server --stdio
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`.
-h, --help output usage information
See configuration documentation.
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,
// or just
"source.organizeImports": true,
}
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.
- Request:
{ command: `_typescript.goToSourceDefinition` arguments: [ lsp.DocumentUri, // String URI of the document lsp.Position, // Line and character position (zero-based) ] }
- Response:
lsp.Location[] | null
(This command is supported from Typescript 4.7.)
- Request:
{ command: `_typescript.applyWorkspaceEdit` arguments: [lsp.WorkspaceEdit] }
- Response:
lsp.ApplyWorkspaceEditResult
- Request:
{ command: `_typescript.applyCodeAction` arguments: [ tsp.CodeAction, // TypeScript Code Action object ] }
- Response:
void
- Request:
{ command: `_typescript.applyRefactoring` arguments: [ tsp.GetEditsForRefactorRequestArgs, ] }
- Response:
void
- Request:
{ command: `_typescript.organizeImports` arguments: [ // The "skipDestructiveCodeActions" argument is supported from Typescript 4.4+ [string] | [string, { skipDestructiveCodeActions?: boolean }], ] }
- Response:
void
- Request:
{ command: `_typescript.applyRenameFile` arguments: [ { sourceUri: string; targetUri: string; }, ] }
- Response:
void
- Request:
{ command: `_typescript.configurePlugin` arguments: [pluginName: string, configuration: any] }
- Response:
void
Code lenses can be enabled using the implementationsCodeLens
and referencesCodeLens
workspace configuration options.
Code lenses provide a count of references and/or implemenations for symbols in the document. For clients that support it it's also possible to click on those to navigate to the relevant locations in the the project. Do note that clicking those trigger a editor.action.showReferences
command which is something that client needs to have explicit support for. Many do by default but some don't. An example command will look like this:
command: {
title: '1 reference',
command: 'editor.action.showReferences',
arguments: [
'file://project/foo.ts', // URI
{ line: 1, character: 1 }, // Position
[ // A list of Location objects.
{
uri: 'file://project/bar.ts',
range: {
start: {
line: 7,
character: 24,
},
end: {
line: 7,
character: 28,
},
},
},
],
],
}
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;
}
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 example4.8.4
)source
- a string specifying whether used TypeScript version comes from the local workspace (workspace
), is explicitly specified through ainitializationOptions.tsserver.path
setting (user-setting
) or was bundled with the server (bundled
)
yarn build
Build and rebuild on change.
yarn dev
yarn test
- run all tests in watch mode for developingyarn test:commit
- run all tests once
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.
The project uses https://github.com/google-github-actions/release-please-action Github action to automatically release new version on merging a release PR.