-
Notifications
You must be signed in to change notification settings - Fork 1
server API
Creates a new instance of TypeScript Server.
Returns Object An object containing methods to interact with TypeScript Server.
Initializes the TypeScript Server instance.
This function starts a new process for the TypeScript Server using Node's child_process.spawn.
It sets up listeners for the 'stdout' and 'stderr' streams of the TypeScript Server process,
handling incoming data and errors respectively. It also handles the 'close' event of the process.
The function configures a timeout to reject the promise if the server does not start within a specified time.
-
nodestring The path to the Node.js executable. If not provided, defaults to 'node'. (optional, default"") -
tsServerstring The path to the TypeScript Server executable. If not provided, defaults to the 'tsserver' path in the 'node_modules' directory. (optional, default"")
Returns Promise<void> A promise that resolves when the TypeScript Server is ready, or rejects if there is an error or timeout.
Handles incoming data from the TypeScript Server.
-
rawDatastring Raw data received from tsserver.
Processes a complete message from tsserver.
-
messagestring A complete message in JSON format.
Checks if the buffer has a complete message based on Content-Length.
Returns boolean True if a complete message is present in the buffer.
Sends a command to the TypeScript Server. Special handling for 'open' command as it does not receive a response.
-
commandObject The command object to send. -
timeoutnumber The timeout in milliseconds for the command. (optional, default5000)
Returns Promise<Object> A promise that resolves with the response from tsserver.
Sends an 'open file' command to the TypeScript Server.
-
filePathstring The path to the TypeScript file to open. -
timeoutnumber The timeout in milliseconds for the command. (optional, default5000)
Returns Promise<Object> A promise that resolves with the response from tsserver.
Sends a 'change' command to the TypeScript Server.
-
filePathstring The path to the file. -
startObject The start position of the change (line and offset). -
endObject The end position of the change (line and offset). -
newTextstring The new text to replace in the range.
Sends a 'close' command to the TypeScript Server.
-
filePathstring The path to the file being closed.
Sends a 'definition' request to the TypeScript Server.
-
filePathstring The path to the file. -
linenumber The line number of the position. -
offsetnumber The offset in the line of the position.
Returns Promise<Object> A promise that resolves with the response from tsserver.
Sends a 'quickinfo' request to the TypeScript Server.
-
filePathstring The path to the file. -
linenumber The line number of the position. -
offsetnumber The offset in the line of the position.
Returns Promise<Object> A promise that resolves with the response from tsserver.
Sends a 'references' request to the TypeScript Server. This command is used to find all references to a symbol at a specified position in a file. It's commonly used to identify where a variable, function, class, or other symbol is used throughout the codebase.
-
filePathstring The path to the TypeScript file. -
linenumber The line number where the symbol is located. -
offsetnumber The character offset (position) in the line where the symbol is located.
Returns Promise<Array<Object>> A promise that resolves with an array of reference items.
Each item represents a reference to the symbol and includes:* file: The file in which the reference occurs.
-
start: The starting position of the reference (line and character). -
end: The ending position of the reference (line and character). -
lineText: The text of the line containing the reference. -
isWriteAccess: A boolean indicating if the reference is a write access (modification). -
isDefinition: A boolean indicating if the reference is the definition of the symbol.Example usage: references('path/to/file.ts', 10, 5).then(refs => { console.log('Symbol references:', refs); });This function is crucial for understanding how and where symbols are used in a project, facilitating code comprehension and refactoring.
Sends a 'findSourceDefinition' request to the TypeScript Server. This command is utilized to locate the original source definition of a symbol at a given position in a TypeScript file. It is particularly useful for tracing the origin of symbols, especially in cases where symbols are re-exported, helping developers navigate to the actual definition rather than a re-exported reference.
-
filePathstring The path to the TypeScript file. -
linenumber The line number where the symbol whose definition is to be found is located. -
offsetnumber The character offset within the line where the symbol is located.
Returns Promise<Object> A promise that resolves with the location information of the symbol's
source definition. The response object typically includes:
- file: String indicating the file path where the source definition is located.
- start: Object representing the start position of the definition, containing:
- line: The line number of the start position (1-based).
- offset: The character offset at the start line (1-based).
- end: Object representing the end position of the definition, similar in structure
to the start object.Example usage: findSourceDefinition('path/to/file.ts', 10, 5).then(definition => {
console.log('Source definition location:', definition);
});This function is essential for developers in complex TypeScript projects, providing a means to
quickly navigate to the original declaration of symbols, enhancing code understanding and navigation.
Sends a 'completionInfo' command to the TypeScript Server.
-
filePathstring The path to the file. -
linenumber The line number of the position. -
offsetnumber The offset in the line of the position.
Returns Promise<Object> A promise that resolves with the response from tsserver.
Sends a 'completionEntryDetails' request to the TypeScript Server.
-
filePathstring The path to the file. -
linenumber The line number of the position. -
offsetnumber The offset in the line of the position. -
entryNamestring The name of the completion entry.
Returns Promise<Object> A promise that resolves with the response from tsserver.
Sends a 'compileOnSaveAffectedFileList' request to the TypeScript Server.
-
filePathstring The path to the file.
Returns Promise<Object> A promise that resolves with the response from tsserver.
Sends a 'compileOnSaveEmitFile' command to the TypeScript Server.
-
filePathstring The path to the TypeScript file. -
forcedboolean Force emit even if there are errors. (optional, defaultfalse) -
includeLinePositionboolean Include line position in the response. (optional, defaultfalse) -
richResponseboolean If true, returns response as an object with detailed emit results. (optional, defaultfalse)
Returns Promise<(boolean | EmitResult)> A promise that resolves with a boolean indicating success
or an EmitResult object containing detailed information about the emit process.
- If a boolean: true if the emit was successful, false otherwise.
- If an EmitResult object:
- emitSkipped: A boolean indicating whether the emit was skipped.
- diagnostics: An array of Diagnostic or DiagnosticWithLinePosition objects, providing
detailed information about each diagnostic message.
- Diagnostic: An object representing a diagnostic message, typically containing:
- start: The starting position of the diagnostic message.
- length: The length of the diagnostic message.
- text: The text of the diagnostic message.
- DiagnosticWithLinePosition: An extended version of Diagnostic, including line and
character position information:
- start: The starting position of the diagnostic message (line and character).
- end: The ending position of the diagnostic message (line and character).
- text: The text of the diagnostic message.
Sends a 'definitionAndBoundSpan' request to the TypeScript Server.
-
filePathstring The path to the file. -
linenumber The line number of the position. -
offsetnumber The offset in the line of the position.
Returns Promise<Object> A promise that resolves with the response from tsserver.
Sends an 'implementation' request to the TypeScript Server.
-
filePathstring The path to the file. -
linenumber The line number of the position. -
offsetnumber The offset in the line of the position.
Returns Promise<Object> A promise that resolves with the response from tsserver.
Sends a 'format' request to the TypeScript Server.
-
filePathstring The path to the file. -
startLinenumber The starting line number of the format range. -
startOffsetnumber The starting offset in the start line. -
endLinenumber The ending line number of the format range. -
endOffsetnumber The ending offset in the end line. -
formatOptionsobject? Optional formatting options. (optional, default{})
Returns Promise<Object> A promise that resolves with the response from tsserver.
Sends an 'exit' command to the TypeScript Server to gracefully shut it down.
Sends a 'formatonkey' request to the TypeScript Server. This command is used for formatting code at a specific position in a file, typically in response to a keystroke. It's commonly used for auto-formatting a line of code when a specific key (like a closing brace or semi-colon) is pressed.
-
filePathstring The path to the TypeScript file. The path should be absolute or relative to the TypeScript server's current working directory. -
linenumber The 1-based line number in the file where the key was pressed. This and the offset together point to the specific position in the file. -
offsetnumber The 1-based character offset in the line where the key was pressed. This is typically the position right after where the key was pressed. -
keystring The character corresponding to the key pressed. This is typically a single character like ';' or '}' that triggers the formatting action. -
formatOptionsobject? Optional formatting options to customize the formatting behavior. These options might include settings like tab size, indent size, whether to insert spaces, and so on. Example: { tabSize: 4, indentSize: 4, insertSpace: true } (optional, default{})
Returns Promise<Object> A promise that resolves with the response from tsserver. The response typically includes an array of code edits that should be applied to the file to achieve the desired formatting. Each edit suggests changes like text insertions, deletions, or replacements.
Sends a 'geterr' request to the TypeScript Server. This command instructs the server to compute and return errors (diagnostics) for the specified files. The diagnostics are not returned directly by this function but are instead sent back by the server as separate events or messages. This function is useful for asynchronously obtaining diagnostic information like errors and warnings from the server.
-
filePathsArray<string> An array of paths to the files for which to get errors. Each path should be either absolute or relative to the TypeScript server's current working directory. -
delaynumber The delay in milliseconds to wait before the server processes the request. This delay can be used to batch or throttle error requests, especially when dealing with a large number of file changes or edits.
Returns Promise<void> A promise that resolves when the command has been sent to the server. The resolution of this promise indicates that the request was successfully dispatched, but it does not imply that the errors have been received. The actual errors (diagnostics) will be sent back by the server asynchronously as separate events or messages, which should be handled separately in the client's message handling logic.Example usage: getErrors(['path/to/file1.ts', 'path/to/file2.ts'], 500).then(() => { console.log('Error request sent. Waiting for diagnostics...'); });Note: The client should implement additional logic to listen for and handle the diagnostic events or messages sent by the server in response to this request.
Sends a 'geterrForProject' request to the TypeScript Server. This command instructs the server to compute and return errors (diagnostics) for all files in a specific project. The diagnostics are not returned directly by this function but are instead sent back by the server as separate events or messages. This function is useful for asynchronously obtaining a comprehensive diagnostic overview of an entire project.
-
filePathstring The path to any file within the project. The server uses this file to identify the project context. The path should be absolute or relative to the TypeScript server's current working directory. -
delaynumber The delay in milliseconds before the server processes the request. This delay can be used to batch or throttle diagnostic requests, especially useful when dealing with large projects or numerous file changes.
Returns Promise<void> A promise that resolves when the command has been sent to the server. The resolution of this promise indicates that the request was successfully dispatched, but it does not imply that the errors have been received. The actual errors (diagnostics) for the entire project will be sent back by the server asynchronously as separate events or messages, which should be handled separately in the client's message handling logic.Example usage: getErrorsForProject('path/to/anyFileInProject.ts', 500).then(() => { console.log('Project error request sent. Waiting for diagnostics...'); });Note: The client should implement additional logic to listen for and handle the diagnostic events or messages sent by the server in response to this request. These diagnostics will cover the entire scope of the project associated with the provided file path.
Sends a 'semanticDiagnosticsSync' request to the TypeScript Server. This command is used to synchronously request semantic diagnostics (such as type errors) for a specific file. It's useful when immediate and up-to-date semantic error information is needed for a file, such as during file saves or build operations.
-
filePathstring The path to the TypeScript file for which semantic diagnostics are requested. The path should be absolute or relative to the TypeScript server's current working directory. -
includeLinePositionboolean If set to true, the response will include detailed line and character position information for each diagnostic. This is useful for integrations that require precise location data, such as IDEs or advanced text editors. (optional, defaultfalse)
Returns Promise<Object> A promise that resolves with the semantic diagnostics response from tsserver.
The response includes an array of diagnostic objects, each representing a
semantic error or warning found in the file. Each diagnostic object typically
contains the following information:
- start: The starting position of the error (line and character).
- length: The length of the error in characters.
- text: The error message text.
- category: The error category ('error', 'warning', or 'suggestion').
- code: The error code (useful for further reference or lookups).
If includeLinePosition is true, additional line and character position
information will be included in each diagnostic.Example usage: getSemanticDiagnosticsSync('path/to/file.ts', true).then(response => {
console.log('Semantic diagnostics with line positions:', response);
}).catch(error => {
console.error('Error getting semantic diagnostics:', error);
});Note: This function performs a synchronous request, meaning it waits for the TypeScript server
to compute and return the diagnostics. The response is directly related to the current
state of the file at the time of the request.
Sends a 'syntacticDiagnosticsSync' request to the TypeScript Server. This command is used to synchronously obtain syntactic diagnostic information (like parsing errors) for a specified file. Syntactic diagnostics are concerned with issues related to the parsing of the source code. This function is particularly useful for quickly identifying syntax errors in a TypeScript file.
-
filePathstring The path to the TypeScript file. The path should be absolute or relative to the TypeScript server's current working directory. -
includeLinePositionboolean Specifies whether to include line and character position information in the diagnostics. When set to true, each diagnostic includes detailed position information, which is useful for displaying errors directly in an editor. (optional, defaultfalse)
Returns Promise<Object> A promise that resolves with the response from tsserver containing syntactic
diagnostics. The response is an array of diagnostic objects. Each diagnostic object
typically contains:
- start: The starting position of the diagnostic message.
- length: The length of the diagnostic message.
- text: The text of the diagnostic message.
If includeLinePosition is true, the diagnostic object also includes:
- startLocation: An object with line and character position of the start.
- endLocation: An object with line and character position of the end.Example usage: getSyntacticDiagnosticsSync('path/to/file.ts', true).then(response => {
console.log('Syntactic diagnostics:', response);
});
Sends a 'suggestionDiagnosticsSync' request to the TypeScript Server. This command is used to synchronously obtain suggestion diagnostic information for a specified file. Suggestion diagnostics include tips and hints that may not necessarily be errors or warnings but could suggest improvements or best practices in the code.
-
filePathstring The path to the TypeScript file. This should be an absolute path or relative to the TypeScript server's current working directory. -
includeLinePositionboolean Specifies whether to include line and character position information in the diagnostics. When set to true, each diagnostic includes detailed position information, which is useful for displaying suggestions directly in an editor. (optional, defaultfalse)
Returns Promise<Object> A promise that resolves with the response from tsserver containing
suggestion diagnostics. The response is typically an array of diagnostic
objects. Each diagnostic object includes:
- start: The starting position of the diagnostic message.
- length: The length of the diagnostic message.
- text: The text of the diagnostic message.
If includeLinePosition is true, the diagnostic object also includes:
- startLocation: An object with line and character position of the start.
- endLocation: An object with line and character position of the end.Example usage: getSuggestionDiagnosticsSync('path/to/file.ts', true).then(response => {
console.log('Suggestion diagnostics:', response);
});This function is particularly useful for tools and editors integrating TypeScript support,
providing an opportunity to present potential code improvements or best practices to the developer.
Sends a 'navbar' request to the TypeScript Server. This command is used to obtain the navigation bar structure of a TypeScript file. The navigation bar typically includes a hierarchical outline of the file's structure, including classes, interfaces, functions, variables, and other code constructs.
-
filePathstring The path to the TypeScript file for which the navigation bar information is requested. The path should be absolute or relative to the TypeScript server's current working directory.
Returns Promise<Object> A promise that resolves with the response from tsserver containing
the navigation bar information. The response is typically an array
of items representing the various code constructs in the file. Each item
includes:
- text: The name of the code construct (e.g., class name, function name).
- kind: The kind of code construct (e.g., 'class', 'function').
- kindModifiers: Modifiers applied to the code construct (e.g., 'public', 'static').
- spans: An array of span objects indicating the location of the construct in the file.
- childItems: An array of child items, following the same structure, representing nested constructs.Example usage: getNavBar('path/to/file.ts').then(response => {
console.log('Navigation bar structure:', response);
});This function is particularly useful for tools and editors integrating TypeScript support,
providing an opportunity to present a structured outline or overview of a code file to the developer.
Sends a 'navto' request to the TypeScript Server. This command is used for searching named symbols in the project or in a particular file, with options to limit the results and scope the search to a specific project.
-
searchValuestring The search term to navigate to from the current location. The term can be '.*' or an identifier prefix. -
filestring? Optional file path to restrict the search to a specific file. -
currentFileOnlyboolean When true, limits search to the current file. (optional, defaultfalse) -
maxResultCountnumber? Optional limit on the number of items to return. -
projectFileNamestring? Optional name of the project file (absolute pathname required).
Returns Promise<Array<Object>> A promise that resolves with an array of navigation items.
Sends a 'navtree' request to the TypeScript Server to obtain the navigation tree of a TypeScript file. The navigation tree provides a hierarchical outline of the file's contents, detailing classes, interfaces, functions, variables, and other top-level constructs. This structured information is useful for understanding the organization of code and for quick navigation within IDEs or editors.
-
filePathstring The absolute path to the TypeScript file. This path is required to locate the file within the project or file system. -
projectFileNamestring? Optional. The absolute path to the project file (usually 'tsconfig.json'). Providing this path helps the TypeScript server correctly resolve the file's context within a specific project, especially useful in workspaces with multiple TypeScript projects.
Returns Promise<Object> A promise that resolves with the navigation tree from the TypeScript server.
The tree is a hierarchical object with nodes representing various code constructs.
Each node typically includes:
- text: The name of the construct (e.g., class or function name).
- kind: The kind of construct (e.g., 'class', 'function').
- spans: Array of location spans indicating where the construct appears in the file.
- childItems: Array of child nodes for nested constructs (following the same structure).Example usage: getNavTree('path/to/file.ts', 'path/to/project.tsconfig.json').then(navTree => {
console.log('Navigation tree:', navTree);
});The returned navigation tree is especially valuable in development environments where a visual outline
or structure of the code file is beneficial for navigation and code comprehension.
Sends a 'navtree-full' request to the TypeScript Server. This command obtains a comprehensive navigation tree of a TypeScript file, which provides a detailed outline of the file's structure. The response includes an extensive hierarchy of all symbols and their nested scopes within the file, such as classes, interfaces, functions, variables, and other code constructs.
This detailed navigation tree is particularly useful for applications that require an in-depth understanding of the file's structure, such as advanced IDE features for code navigation and analysis.
-
filePathstring The absolute path to the TypeScript file for which the full navigation tree is requested. This path is essential for the TypeScript server to locate and analyze the file.
Returns Promise<Object> A promise that resolves with the full navigation tree from the TypeScript server.
The tree is represented as an object with a hierarchical structure. Each node in the tree
includes:
- text: The name of the item (e.g., a class or function name).
- kind: The kind of item (e.g., 'class', 'function').
- spans: An array of span objects indicating the location of the item in the file.
- childItems: An array of child nodes representing nested declarations and structures.
Each child node follows the same structure.Example usage: getNavTreeFull('path/to/file.ts').then(navTreeFull => {
console.log('Full navigation tree:', navTreeFull);
});
Sends a 'documentHighlights' request to the TypeScript Server. This command is used to obtain highlights of all occurrences of a symbol within a specified set of files, optionally within the context of a specific project. It is useful for identifying and navigating to instances of a variable, function name, or other identifiers across multiple files.
-
filePathstring The path to the TypeScript file where the symbol occurs. -
linenumber The line number where the symbol is located. -
offsetnumber The character offset (position) in the line where the symbol is located. -
filesToSearchArray<string> The list of file paths to search for document highlights. -
projectFileNamestring? Optional. The name of the project file (absolute pathname required) that contains the TypeScript file. Providing this helps to accurately resolve symbols in the context of the given project. (optional, default"")
Returns Promise<Array<Object>> A promise that resolves with an array of document highlight objects.
Each object represents a file with highlight instances and includes:* file: The file in which the highlight occurs.
-
highlightSpans: An array of objects representing the highlight locations. Each object includes:-
start: The starting position of the highlight (line and character). -
end: The ending position of the highlight (line and character). -
kind: The kind of highlight (e.g., 'writtenReference', 'reference', 'definition').Example usage: documentHighlights('path/to/file.ts', 10, 5, ['path/to/file1.ts', 'path/to/file2.ts'], 'path/to/project.tsconfig.json') .then(highlights => { console.log('Document highlights:', highlights); });This function is essential for features like symbol search in development environments, where highlighting symbol occurrences enhances code understanding and navigation.
-
Sends a 'reload' request to the TypeScript Server to reload the contents of a file from disk. This command is useful for ensuring the server's view of a file is synchronized with the latest content, particularly after external changes to the file.
-
filePathstring The path to the file to be reloaded. -
tempFilePathstring The path to a temporary file that contains the new content. This allows reloading the file content without modifying the original file on disk. -
projectFileNamestring? Optional. The name of the project file (absolute pathname required) that contains the TypeScript file. Providing this helps the TypeScript server accurately interpret the file in the context of the specified project. (optional, default"")
Returns Promise<Object> A promise that resolves with the server's response after the file has been reloaded. The response typically includes a status indicating whether the reload was successful.Example usage: reload('path/to/file.ts', 'path/to/tempFile.ts', 'path/to/project.tsconfig.json').then(response => { console.log('File reload response:', response); });This function is essential in development environments where files are frequently modified outside the editor and need to be synchronized with the TypeScript server.
Sends a 'rename' request to the TypeScript Server to perform a comprehensive renaming operation for a symbol at a specified location in a file. It updates references to the symbol across the entire codebase, including in comments and strings if specified.
-
filePathstring The path to the TypeScript file. -
linenumber The line number where the symbol to be renamed is located. -
offsetnumber The character offset (position) in the line where the symbol is located. -
findInCommentsboolean Whether to find/change the text in comments. (optional, defaultfalse) -
findInStringsboolean Whether to find/change the text in strings. (optional, defaultfalse)
Returns Promise<Object> A promise that resolves with the rename information from tsserver.
The response object includes:
- canRename: A boolean indicating if the symbol can be renamed.
- locs: An array of location objects where each object represents
a file with references to the symbol. Each location object includes:
- file: The file in which references are found.
- locs: An array of span objects representing the reference locations.
Each span object includes:
- start: The starting line and character position of the reference.
- end: The ending line and character position of the reference.
- displayName: The full display name of the symbol.
- fullDisplayName: The full display name of the symbol with container information.
- kind: The kind of the symbol (e.g., 'variable', 'function').
- kindModifiers: The kind modifiers of the symbol (e.g., 'public', 'static').Example usage: rename('path/to/file.ts', 10, 5, true, true).then(renameInfo => {
console.log('Rename information:', renameInfo);
});This function is essential for refactoring, providing a safe and consistent way to rename symbols
across a project, including their occurrences in comments and strings if required.
Sends a 'saveto' request to the TypeScript Server. This command instructs the server to save the server's current view of a file's contents to a specified temporary file. It's primarily used for debugging purposes. Note that the server does not send a response to a "saveto" request.
-
filePathstring The path to the original TypeScript file. -
tempFilePathstring The path where the server's view of the file's current contents should be saved. -
projectFileNamestring? Optional. The name of the project file (absolute pathname required) that contains the TypeScript file.Example usage: saveto('path/to/originalFile.ts', 'path/to/tempFile.ts', 'path/to/project.tsconfig.json');This command is useful for debugging, allowing the current state of the file as seen by the TypeScript server to be saved to a specific location. (optional, default'')
Sends a 'signatureHelp' request to the TypeScript Server. This command is used to obtain information about the signature of a function or method at a specific position in a file. The response includes details about the function signatures and their parameters.
-
filePathstring The path to the TypeScript file. -
linenumber The line number where the function or method is invoked. -
offsetnumber The character offset in the line where the invocation occurs. -
triggerReasonObject? The reason why signature help was invoked, with properties: -kind: The type of trigger reason ('invoked', 'characterTyped', 'retrigger'). -triggerCharacter: The character that triggered the help (for 'characterTyped').
Returns Promise<Object> A promise that resolves with the signature help information, which includes:
- items: Array of objects representing each signature. Each object includes:
- label: String representation of the function signature.
- documentation: Optional documentation for the function.
- parameters: Array of parameter information objects, each with:
- label: The parameter name.
- documentation: Optional documentation for the parameter.
- applicableSpan: Object representing the span for which signature help is applicable.
- selectedItemIndex: Number indicating the default selected signature.
- argumentIndex: Number indicating the index of the argument where the cursor is located.
- argumentCount: Number indicating the total number of arguments in the function call.Example usage: signatureHelp('path/to/file.ts', 15, 10, { kind: 'characterTyped', triggerCharacter: '(' }).then(help => {
console.log('Signature help:', help);
});This function is essential for providing inline function/method signature information in development environments.
Sends a 'status' request to the TypeScript Server. This command queries the current status of the server, providing information about its operational state. This can include details such as the server's version, the number of projects currently loaded, and any ongoing operations.
Returns Promise<Object> A promise that resolves with the status information from tsserver. The response typically includes details about the server's state, including its version and the status of loaded projects.Example usage: status().then(serverStatus => { console.log('TypeScript server status:', serverStatus); });This function is useful for monitoring the TypeScript server and diagnosing issues with its operation.
Sends a 'typeDefinition' request to the TypeScript Server. This command is used to find the type definition of a symbol at a specified location in a TypeScript file. It is useful for navigating to the definition of the type of a symbol, such as the type of a variable, parameter, or property.
-
filePathstring The path to the TypeScript file. -
linenumber The line number where the symbol is located. -
offsetnumber The character offset (position) in the line where the symbol is located.
Returns Promise<Object> A promise that resolves with the location of the symbol's type definition.
The response typically includes:
- file: The file path of the type definition.
- start: The starting position of the type definition (line and character).
- end: The ending position of the type definition (line and character).Example usage: typeDefinition('path/to/file.ts', 10, 5).then(definition => {
console.log('Type definition location:', definition);
});This function is crucial for understanding and navigating to the types used in a TypeScript codebase.
Sends a 'projectInfo' request to the TypeScript Server to retrieve information about the TypeScript project associated with a specific file. This includes details about the project's configuration, the files included, and the status of the language service.
-
filePathstring The path to the TypeScript file. -
needFileNameListboolean Indicates whether the list of file names in the project is needed. (optional, defaultfalse) -
projectFileNamestring? Optional. The name of the project file (absolute pathname required) that contains the TypeScript file. (optional, default"")
Returns Promise<Object> A promise that resolves with the project information, which includes:
- configFileName: A string representing the path to the project's
configuration file (tsconfig.json), if available.
- fileNames: An array of strings representing the file names in the project,
included if needFileNameList is true.
- languageServiceDisabled: A boolean indicating whether the language service
is disabled for this project.Example usage: projectInfo('path/to/file.ts', true, 'path/to/project.tsconfig.json').then(info => {
console.log('Project information:', info);
});This function is useful for tools and IDEs to gain insights into the structure and configuration
of a TypeScript project.
Sends a 'reloadProjects' request to the TypeScript Server. This command instructs the server to reload all projects it has loaded. It is particularly useful when project configurations or file structures have changed, ensuring that tsserver is synchronized with the current state of the projects.
Returns Promise<void> A promise that resolves when the server has reloaded the projects. Note that there is no direct response from the server for this command, so the promise resolves as soon as the command is sent.Example usage: reloadProjects().then(() => { console.log('All projects reloaded'); });This function is essential for maintaining project synchronization with tsserver, particularly after significant changes to project configurations or file structures.
Sends an 'openExternalProject' request to the TypeScript Server. This command opens an external project with the provided configuration, which includes the project's file name, root files, compiler options, and type acquisition settings. The server responds with an acknowledgement.
-
projectObject The external project configuration, which includes: -projectFileName: The name or path of the project file. -rootFiles: An array of objects representing the root files in the project. Each object includesfileNameand other optional properties likescriptKindandhasMixedContent. -options: The compiler options for the project. -typeAcquisition: Optional type acquisition settings for the project.
Returns Promise<Object> A promise that resolves when the server has acknowledged the opening of the external project.
The response object contains standard response fields such as:
- success: A boolean indicating whether the request was successful.
- request_seq: The sequence number of the request.
- command: The command requested.
- message: An optional success or error message.Example usage: const externalProject = {
projectFileName: 'path/to/external/project',
rootFiles: [{ fileName: 'path/to/rootFile1.ts', scriptKind: 'ts', hasMixedContent: true }],
options: { noImplicitAny: true, strictNullChecks: true },
typeAcquisition: { enable: true, include: ['node'] }
};
openExternalProject(externalProject).then(response => {
console.log('External project opened:', response);
});This function is particularly useful for integrating tsserver with environments where
project configurations are defined externally.
Sends an 'openExternalProjects' request to the TypeScript Server. This command is used to open multiple external projects simultaneously. Each project configuration includes the project's file name, root files, compiler options, and type acquisition settings. The server responds with an acknowledgement.
-
projectsArray<Object> An array of external project configurations. Each configuration includes: -projectFileName: The name or path of the project file. -rootFiles: An array of objects representing the root files in the project. -options: The compiler options for the project. -typeAcquisition: Optional type acquisition settings for the project.
Returns Promise<Object> A promise that resolves when the server has acknowledged
opening the external projects. The response object contains
standard response fields like:
- success: A boolean indicating whether the request was successful.
- request_seq: The sequence number of the request.
- command: The command requested.
- message: An optional success or error message.Example usage: const externalProjects = [
{
projectFileName: 'path/to/external/project1',
rootFiles: [{ fileName: 'path/to/rootFile1.ts', scriptKind: 'ts', hasMixedContent: true }],
options: { noImplicitAny: true },
typeAcquisition: { enable: true, include: ['node'] }
},
// ... more projects ...
];
openExternalProjects(externalProjects).then(response => {
console.log('External projects opened:', response);
});This function is especially useful for environments where multiple TypeScript projects are managed externally.
- See: exitServer - Sends an 'exit' command to the TypeScript Server for a graceful shutdown.
Terminates the TypeScript Server process. Warning: Use this function with caution. Prefer using the exitServer function for a graceful shutdown.