get-tsconfig is a zero-dependency TypeScript library for finding and parsing tsconfig.json files. It provides comprehensive tools for locating TypeScript configuration files through directory traversal, parsing them with support for JSON comments and dangling commas, and resolving extends chains. The library includes advanced features like file matching and path resolution, making it ideal for TypeScript tooling and build systems.
npm install get-tsconfigimport { getTsconfig, parseTsconfig, createFilesMatcher, createPathsMatcher, type TsConfigResult, type TsConfigJsonResolved, type FileMatcher, type Cache } from "get-tsconfig";For CommonJS:
const { getTsconfig, parseTsconfig, createFilesMatcher, createPathsMatcher } = require("get-tsconfig");import { getTsconfig, parseTsconfig } from "get-tsconfig";
// Find and parse tsconfig.json starting from current directory
const result = getTsconfig();
if (result) {
console.log("Found tsconfig at:", result.path);
console.log("Compiler options:", result.config.compilerOptions);
}
// Find tsconfig.json from a specific path
const configFromPath = getTsconfig("./src/components");
// Parse a specific tsconfig file
const customConfig = parseTsconfig("./tsconfig.build.json");get-tsconfig is built around several key components:
getTsconfig and parseTsconfig for finding and parsing TypeScript configurationscreateFilesMatcher for determining which files match tsconfig include/exclude patternscreatePathsMatcher for resolving module specifiers using compilerOptions.pathsCore functionality for finding and parsing TypeScript configuration files with full support for extends resolution and JSON with comments.
function getTsconfig(
searchPath?: string,
configName?: string,
cache?: Cache
): TsConfigResult | null;
function parseTsconfig(
tsconfigPath: string,
cache?: Cache<string>
): TsConfigJsonResolved;
interface TsConfigResult {
path: string;
config: TsConfigJsonResolved;
}
type TsConfigJsonResolved = Except<TsConfigJson, 'extends'>;
type Cache<T = any> = Map<string, T>;Determines whether files match TypeScript configuration include/exclude patterns, essential for build tools and file processors.
function createFilesMatcher(
tsconfig: TsConfigResult,
caseSensitivePaths?: boolean
): FileMatcher;
type FileMatcher = (filePath: string) => (TsConfigJsonResolved | undefined);Resolves module specifiers using TypeScript's path mapping configuration, compatible with compilerOptions.paths and baseUrl settings.
function createPathsMatcher(
tsconfig: TsConfigResult
): ((specifier: string) => string[]) | null;// Re-exported from type-fest for complete TypeScript configuration schema
type TsConfigJson = {
extends?: string | string[];
compilerOptions?: CompilerOptions;
files?: string[];
include?: string[];
exclude?: string[];
references?: ProjectReference[];
// ... complete TypeScript configuration schema
};
// Resolved configuration with extends processed
type TsConfigJsonResolved = Except<TsConfigJson, 'extends'>;
// Result object from getTsconfig
interface TsConfigResult {
/** The path to the tsconfig.json file */
path: string;
/** The resolved tsconfig.json file */
config: TsConfigJsonResolved;
}
// Generic cache type for performance optimization
type Cache<value = any> = Map<string, value>;
// File matcher function type
type FileMatcher = (filePath: string) => (TsConfigJsonResolved | undefined);