Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

TypeScript & JavaScript Language Server

License

NotificationsYou must be signed in to change notification settings

typescript-language-server/typescript-language-server

Repository files navigation

Discordnpm versionnpm downloads

TypeScript Language Server

What is it, exactly?

TheTypeScript project/package includes atsserver component which provides a custom API that can be used for gathering various intelligence about a typescript/javascript project. TheVSCode team has built a project calledTypescript Language Features (and bundled it as an internal extension in VSCode) that provides code intelligence for your javascript and typescript projects by utilizing thattsserver API. Since that extension doesn't use the standardizedLanguage Server Protocol to communicate with the editor, other editors that implement LSP can't directly utilize it. Here is where theTypeScript Language Server project comes in with the aim to provide a thin LSP interface on top of that extension's code base for the benefit of all other editors that implement the LSP protocol.

Originally based on concepts and ideas fromhttps://github.com/prabirshrestha/typescript-language-server and maintained byTypeFox. Currently maintained by acommunity of contributors like you.

This project is not directly associated with Microsoft and is not used in theirVSCode editor. If you have an issue with VSCode functionality, report it in their repository instead.

Currently Microsoft is working onTypeScript 7 written natively in the go language that will include the LSP implementation and will hopefully supersede this project.

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`.    -h, --help                             output usage information

Configuration

Seeconfiguration documentation.

Features

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 interface
  • source.removeUnused.ts - removes declared but unused variables
  • source.addMissingImports.ts - adds imports for used but not imported symbols
  • source.removeUnusedImports.ts - removes unused imports
  • source.sortImports.ts - sorts imports
  • source.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,}

Workspace commands (workspace/executeCommand)

SeeLSP specification.

Most of the time, you'll execute commands with arguments retrieved from another request liketextDocument/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

Request:

{    command:'_typescript.goToSourceDefinition'    arguments:[lsp.DocumentUri,// String URI of the documentlsp.Position,// Line and character position (zero-based)]}

Response:

lsp.Location[]|null

(This command is supported from Typescript 4.7.)

Apply Refactoring

Request:

{    command:'_typescript.applyRefactoring'    arguments:[tsp.GetEditsForRefactorRequestArgs,]}

Response:

void

Organize Imports

Request:

{    command:'_typescript.organizeImports'    arguments:[string,// file path// Optional options:{//@deprecated - use "mode". Supported from Typescript 4.4+.skipDestructiveCodeActions?: boolean// 'All' - organizes imports including destructive actions (removing unused imports)// 'SortAndCombine' - Doesn't perform destructive actions.// 'RemoveUnused' - Only removes unused imports.mode?:'All'|'SortAndCombine'|'RemoveUnused'},]}

Response:

void

Rename File

Request:

{    command:'_typescript.applyRenameFile'    arguments:[{sourceUri:string; targetUri:string;},]}

Response:

void

Send Tsserver Command

Request:

{    command:'typescript.tsserverRequest'    arguments:[string,// commandany,// command arguments in a format that the command expectsExecuteInfo,// configuration object used for the tsserver request (see below)]}

Response:

any

TheExecuteInfo object is defined as follows:

typeExecuteInfo={executionTarget?:number;// 0 - semantic server, 1 - syntax server; default: 0expectsResult?:boolean;// default: trueisAsync?:boolean;// default: falselowPriority?:boolean;// default: true};

Configure plugin

Request:

{    command:'_typescript.configurePlugin'    arguments:[pluginName: string,configuration: any]}

Response:

void

Code Lenses (textDocument/codeLens)

Code lenses can be enabled using theimplementationsCodeLens andreferencesCodeLensworkspace configuration options.

Code lenses provide a count ofreferences and/orimplemenations 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 aeditor.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,},},},],],}

Inlay hints (textDocument/inlayHint)

For the request to return any results, some or all of the following options need to be enabled throughpreferences:

exportinterfaceInlayHintsOptionsextendsUserPreferences{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 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)

Workspace Configuration request for formatting settings

Server asks the client for file-specific configuration options (tabSize andinsertSpaces) that are required bytsserver to properly format file edits when for example using "Organize imports" or performing other file modifications. Those options have to be dynamically provided by the client/editor since the values can differ for each file. For this reason server sends aworkspace/configuration request withscopeUri equal to file's URI andsection equal toformattingOptions. The client is expected to return a configuration that includes the following properties:

{"tabSize":number"insertSpaces":boolean}

Development

Build

yarn build

Dev

Build and rebuild on change.

yarn dev

Test

  • yarn test - run all tests in watch mode for developing
  • yarn test:commit - run all tests once

By default only console logs of levelwarning and higher are printed to the console. You can override theCONSOLE_LOG_LEVEL level inpackage.json to eitherlog,info,warning orerror to log other levels.

Publishing

The project useshttps://github.com/google-github-actions/release-please-action Github action to automatically release new version on merging a release PR.


[8]ページ先頭

©2009-2025 Movatter.jp