TypeScript Configuration
Relevant source files
The following files were used as context for generating this wiki page:
The TypeScript configuration in htmlparser2 defines the environment, module resolution strategy, and strictness levels required to maintain a fast, high-performance HTML/XML parser. The project uses a modern ESM (ECMAScript Module) approach, targeting es2022 and utilizing nodenext for module resolution to ensure compatibility with modern Node.js environments.
Sources: tsconfig.json:4-6, package.json:28
Core Compiler Options
The primary configuration is managed in tsconfig.json. It specifies how the TypeScript compiler (tsc) should transform source code in the src directory into executable JavaScript in the dist directory.
Build and Output Settings
The project is configured to emit declaration files and source maps, facilitating debugging and providing type definitions for consumers of the library.
| Option | Value | Description |
|---|---|---|
target | es2022 | Specifies the JavaScript language version for emitted JavaScript. |
module | nodenext | Sets the module system for the program. |
moduleResolution | nodenext | Determines how modules are looked up. |
outDir | dist | Redirects output structure to the directory. |
declaration | true | Generates corresponding .d.ts files. |
declarationMap | true | Generates a sourcemap for each corresponding '.d.ts' file. |
Sources: tsconfig.json:4-11
Strictness and Safety
The project enforces high code quality through a variety of strict checking flags. This ensures that potential runtime errors, such as null references or unused variables, are caught at compile time.
The diagram shows the hierarchy of type-checking strictness applied to the codebase.
Sources: tsconfig.json:14-30
Project Structure and Scoping
The TypeScript configuration is scoped primarily to the src folder. Specific patterns are excluded to prevent test files and fixtures from being included in the production build output.
- Included Path:
src - Excluded Patterns:
**/*.spec.ts(Unit tests)**/__fixtures__/*(Test data/documents)**/__tests__/***/__snapshots__/*
Sources: tsconfig.json:33-39
Linting and Formatting Integration
TypeScript configuration works in tandem with Biome and ESLint to enforce coding standards.
ESLint Configuration
A specialized configuration, tsconfig.eslint.json, extends the base settings specifically for linting purposes. It sets noEmit: true to allow ESLint to perform type-aware linting without generating build artifacts and expands the rootDir to include the entire project structure.
Sources: tsconfig.eslint.json:1-8
Build Scripts
The project defines several npm scripts in package.json that utilize the TypeScript compiler:
| Script | Command | Purpose |
|---|---|---|
build | tsc | Compiles the project using tsconfig.json. |
lint:ts | tsc --noEmit -p tsconfig.eslint.json | Runs type checking without emitting files. |
prepublishOnly | npm run build | Ensures a fresh build before publishing to NPM. |
Sources: package.json:44-53
Module Exports and Type Definitions
The project is configured as a native ESM package ("type": "module"). The package.json exports map specific entry points to their compiled JavaScript and TypeScript declaration files.
This diagram represents the mapping between public API exports and the physical files generated by the TypeScript compiler.
Sources: package.json:28-41
Summary
The TypeScript configuration of htmlparser2 is designed for a modern development workflow, leveraging es2022 targets and nodenext resolution. By combining strict type-checking in tsconfig.json with specialized configurations for linting and multi-entry point exports, the project ensures high reliability and excellent developer experience for consumers requiring type safety.
Sources: tsconfig.json:4-30, package.json:28-42