Use @vtsls/language-service to interact with TypeScript
mainThe @vtsls/language-service package provides a programmatic interface to TypeScript language features. To use it, you must create a service instance, initialize it with configuration, and open text documents before requesting features like documentSymbol. The service follows the Language Server Protocol (LSP) patterns for parameters and responses.
import { createTSLanguageService } from "@vtsls/language-service";
const service = createTSLanguageService({
clientCapabilities: {},
});
// initialize with configuration
await service.initialize({
typescript: { tsserver: { log: "verbose" } },
});
const uri = "file:///path/to/file.ts";
const fileContent = "";
// file needs to be opened before requesting features
service.openTextDocument({
textDocument: {
uri,
languageId: "typescript",
version: 0,
text: fileContent,
},
});
// see LSP document for the format of params and response
const response = await service.documentSymbol({ textDocument: { uri } });
console.log(response);
// close the service
service.dispose();