TypeScript TQDM Progress Bar
ts-tqdm is a Node.js utility library that provides a progress bar functionality, directly inspired by the popular Python `tqdm` library. It allows developers to easily instrument loops, whether iterating over arrays or a fixed number of iterations, with visual progress feedback in the console. The current stable version is 0.8.6. As a focused utility, its release cadence is typically driven by feature enhancements or bug fixes. Its primary differentiator lies in its TypeScript-first design, ensuring type safety and excellent developer experience for TypeScript projects, while replicating the simple, intuitive API familiar to users of `tqdm` in other ecosystems. It is best suited for command-line applications and scripts where visual progress indication can significantly improve user experience for long-running operations.
Common errors
-
TypeError: (0 , ts_tqdm__WEBPACK_IMPORTED_MODULE_0__.tqdm) is not a function
cause Incorrect import statement, often trying to use a CommonJS `require` syntax or a default import when `tqdm` is a named export.fixEnsure you are using `import { tqdm } from 'ts-tqdm';` for ESM modules. For older Node.js versions or specific configurations, ensure proper transpilation if using ES Modules. -
Argument of type 'string[]' is not assignable to parameter of type 'number | Iterable<unknown>'.
cause The `tqdm` function expects either a number (for fixed iterations) or an `Iterable` (like an array or Set). Passing a non-iterable type or a non-number type will result in a TypeScript error.fixEnsure the argument passed to `tqdm` is an `Iterable` (e.g., `Array`, `Set`, `Map`) or a `number`. For example, `tqdm([1, 2, 3])` or `tqdm(100)`.
Warnings
- gotcha Direct console logging (e.g., `console.log`) inside a `tqdm` loop can interfere with the progress bar's display, causing flickering or incorrect rendering. `tqdm` re-renders the line constantly.
- gotcha When `ts-tqdm` is used in environments without a TTY (e.g., some CI/CD pipelines, background processes without a terminal attached), the progress bar might not render correctly or at all. It relies on terminal capabilities for dynamic updates.
- gotcha The library primarily targets Node.js environments and assumes a standard console output. Using it in browser environments directly is not supported and will likely lead to errors related to Node.js-specific globals or console behaviors.
Install
-
npm install ts-tqdm -
yarn add ts-tqdm -
pnpm add ts-tqdm
Imports
- tqdm
const { tqdm } = require('ts-tqdm');import { tqdm } from 'ts-tqdm'; - tqdm
import tqdm from 'ts-tqdm';
import { tqdm } from 'ts-tqdm';
Quickstart
import { tqdm } from "ts-tqdm";
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
(async () => {
console.log('Starting long process...');
const totalIterations = 50;
// Iterate over a fixed number of iterations
for (let i of tqdm(totalIterations, { description: 'Processing items' })) {
// Simulate an asynchronous task
await delay(50);
if (i % 10 === 0) {
// console.log(` Processed item ${i}`); // Avoid console.log inside tqdm loop if it interferes with progress bar
}
}
console.log('Finished processing items.\n');
const data = Array.from({ length: 25 }, (_, i) => `item-${i}`);
for (let item of tqdm(data, { description: 'Analyzing data' })) {
await delay(100);
// console.log(` Analyzing ${item}`);
}
console.log('Data analysis complete.');
})();