TypeScript Diagnostic Service

2.0.3 · abandoned · verified Sun Apr 19

The `typescript-service` package, currently at version 2.0.3 and last updated in August 2018, provides a programmatic interface to interact with the TypeScript language service. Its primary function is to help retrieve diagnostic messages from TypeScript source files by abstracting the direct usage of the complex `typescript` module API. The library allows users to create a service instance by specifying a `tsconfig.json` file and optional `compilerOptions`, then retrieve diagnostics for specific files. However, due to its age and lack of maintenance for over seven years, it is considered abandoned. This makes it highly improbable to be compatible with current major versions of TypeScript (e.g., TypeScript 6.x) or contemporary Node.js environments, and it has no active release cadence. Its key differentiator of simplifying TypeScript diagnostics is now overshadowed by its severe compatibility issues.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing the TypeScript service and retrieving diagnostic messages for a specific file, also showing access to the underlying `ts.Program`.

import { createService } from 'typescript-service';
import * as path from 'path';

const projectRoot = process.cwd();
const configFile = path.join(projectRoot, 'tsconfig.json');
const exampleFile = path.join(projectRoot, 'src', 'example.ts');

// Ensure a tsconfig.json exists for demonstration
// In a real project, this file would be present.
// For quick testing, you might need to create a minimal tsconfig.json and src/example.ts
// e.g., tsconfig.json: { "compilerOptions": { "target": "es5", "module": "commonjs", "strict": true } }
// e.g., src/example.ts: export const a: string = 123; // This will produce a diagnostic

const service = createService({ configFile });
const diagnostics = service.getDiagnostics(exampleFile);

if (diagnostics.length > 0) {
  console.log(`Diagnostics for ${exampleFile}:`);
  diagnostics.forEach(diagnostic => {
    const message = typeof diagnostic.messageText === 'string'
      ? diagnostic.messageText
      : diagnostic.messageText.messageText;
    console.log(`  [TS${diagnostic.code}] ${message} (Line: ${diagnostic.start?.line + 1}, Char: ${diagnostic.start?.character + 1})`);
  });
} else {
  console.log(`No diagnostics found for ${exampleFile}.`);
}

// You can also get the underlying ts.Program instance
const program = service.getProgram();
console.log(`
TypeScript program contains ${program.getSourceFiles().length} source files.`);

view raw JSON →