TypeScript Diagnostic Service
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
-
TypeError: service.update is not a function
cause Attempting to use the `update` method, which was removed in version 2.0.0.fixThe `update` method was removed. Use `getSourceFile` or `getDiagnostics` which accept an optional `sourceText` parameter to provide file content. -
Error: Cannot find module 'typescript-service' or 'require is not defined'
cause Incorrect module import syntax (e.g., `require` in an ESM context) or the package is not correctly installed/resolved.fixEnsure `npm install typescript-service` is run. For module issues, check your project's `type` in `package.json` and `tsconfig.json`'s `module` and `moduleResolution` settings. If your project is ESM, try `import { createService } from 'typescript-service';`. If CJS, try `const { createService } = require('typescript-service');`. -
TS2345: Argument of type 'string | number | boolean' is not assignable to parameter of type 'string'.
cause Compatibility issues with newer TypeScript versions where `ts.Diagnostic` or other types have changed or become stricter, leading to type mismatches when using the old API.fixThis often indicates a fundamental incompatibility with your current TypeScript version. You may need to either downgrade your TypeScript version to one closer to the package's last update (e.g., TypeScript 3.x), or switch to an actively maintained library. Manual type assertions (`as any`) might temporarily silence errors but won't resolve underlying runtime issues. -
Error: getSourceFile fail fixed if file is not defined in tsconfig
cause The `getSourceFile` method failed because the specified file was not included in the `tsconfig.json`'s scope.fixEnsure all files you intend to process with `typescript-service` are included in the `files`, `include`, or `references` sections of your `tsconfig.json`. Alternatively, provide the `sourceText` parameter to `getDiagnostics` or `getSourceFile` if the file is outside the configured project scope.
Warnings
- breaking The `update` method was removed in version 2.0.0. Code relying on this method will break.
- breaking The package is effectively abandoned, with no updates since August 2018. It is highly unlikely to be compatible with modern TypeScript versions (e.g., TS 5.x, 6.x) or recent Node.js releases, leading to potential type mismatches, runtime errors, or unexpected behavior due to API changes in the TypeScript compiler itself.
- gotcha While the README uses an ESM `import` statement, the package's age and `engines.node` requirement (>=6) suggest it may ship as CommonJS. Consuming it in a pure ESM Node.js project without a build step or explicit module configuration might lead to `require is not defined` or `Cannot find module` errors.
Install
-
npm install typescript-service -
yarn add typescript-service -
pnpm add typescript-service
Imports
- createService
const { createService } = require('typescript-service');import { createService } from 'typescript-service'; - ts.Program, ts.DiagnosticWithLocation
const ts = require('typescript');import * as ts from 'typescript';
Quickstart
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.`);