https://github.com/shiyangzhaoa/tsserver
A lite wrapper around tsserver
https://github.com/shiyangzhaoa/tsserver
tsserver typescript
Last synced: 2 months ago
JSON representation
A lite wrapper around tsserver
- Host: GitHub
- URL: https://github.com/shiyangzhaoa/tsserver
- Owner: shiyangzhaoa
- License: mit
- Created: 2022-09-21T02:21:06.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-14T06:22:47.000Z (over 1 year ago)
- Last Synced: 2025-03-14T03:19:34.528Z (3 months ago)
- Topics: tsserver, typescript
- Language: TypeScript
- Homepage:
- Size: 75.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# TSServer wrapper
A lite wrapper around tsserver
![]()
## Install
```shell
yarn add tsserver-lite
```## Example
For example, your project structure:
```shell
├── src
│ └── test.ts
└──── xxx
``````ts
// src/test.ts
export const number = 123;
export const string = 'abc';
```How to use:
```mjs
// index.mjs
import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';import { client } from 'tsserver-lite';
const updateTSFile = async (info) => {
const fileInfo = Object.assign(
{
changedFiles: [],
closedFiles: [],
openFiles: [],
},
info,
);await client.execute('updateOpen', fileInfo);
};const __dirname = path.dirname(fileURLToPath(import.meta.url));
const filePath = path.resolve(__dirname, './test.ts');
const main = async () => {
client.startService();const openFile = {
file: filePath,
fileContent: fs.readFileSync(filePath, 'utf-8'),
projectRootPath: path.resolve(__dirname, '../'),
scriptKindName: 'ts',
};try {
await updateTSFile({
openFiles: [openFile]
});const result = await client.execute(
'quickinfo',
{
file: filePath,
line: 1,
offset: 15,
},
);
if (result.type === 'response') {
console.log('result---', result);
}
} catch (err) {
console.log(`tsserver error: ${err.message}`);
}client.closeService();
};main().catch(err => {
console.error(err);
process.exit(1);
});
```Try run script:
```shell
node index.mjs
```You will see these logs:
```ts
result: {
seq: 0,
type: 'response',
command: 'quickinfo',
request_seq: 3,
success: true,
body: {
kind: 'const',
kindModifiers: 'export',
start: { line: 1, offset: 14 },
end: { line: 1, offset: 20 },
displayString: 'const number: 123',
documentation: '',
tags: []
}
}
```