What is @types/node?
The @types/node package contains TypeScript type definitions for Node.js. It provides TypeScript interfaces and types for Node.js modules, allowing developers to use TypeScript's static type checking with the Node.js runtime environment. This helps to catch errors at compile time and provides better editor support with features like auto-completion and inline documentation.
What are @types/node's main functionalities?
File System
Type definitions for the File System module allow developers to work with the file system using TypeScript, with methods for reading, writing, and manipulating the file system.
import * as fs from 'fs';
fs.readFile('/path/to/file', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
HTTP Server
Type definitions for the HTTP module enable developers to create HTTP servers and clients with TypeScript, handling requests and responses with proper type annotations.
import * as http from 'http';
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
});
server.listen(3000);
Events
Type definitions for the Events module provide types for event handling, allowing developers to work with event emitters and listeners in a type-safe manner.
import { EventEmitter } from 'events';
const eventEmitter = new EventEmitter();
eventEmitter.on('myEvent', (arg) => {
console.log(`Event fired with argument: ${arg}`);
});
eventEmitter.emit('myEvent', 'Hello Event!');
Streams
Type definitions for the Streams module help developers to work with Node.js streams, providing types for readable, writable, duplex, and transform streams.
import * as fs from 'fs';
import * as stream from 'stream';
const readStream = fs.createReadStream('/path/to/file');
const writeStream = fs.createWriteStream('/path/to/destination');
readStream.pipe(new stream.Transform({
transform(chunk, encoding, callback) {
// Transform the chunk to uppercase
callback(null, chunk.toString().toUpperCase());
}
})).pipe(writeStream);
Child Processes
Type definitions for the Child Processes module allow developers to spawn or execute new processes, providing types for handling the output and errors.
import { exec } from 'child_process';
exec('ls -lh /usr', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
Other packages similar to @types/node
typescript
The TypeScript package is the core compiler for TypeScript. It provides the language services and type checking for TypeScript code but does not include type definitions for Node.js, which @types/node provides.
ts-node
ts-node is a TypeScript execution engine and REPL for Node.js. It allows you to run TypeScript files directly without pre-compiling them. It uses the TypeScript package for type checking but relies on @types/node for Node.js type definitions.
tslib
tslib is a runtime library for TypeScript that includes helper functions used by TypeScript emitted code. It does not provide type definitions but is often used alongside @types/node in TypeScript projects to reduce code duplication.
Installation
npm install --save @types/node
Summary
This package contains type definitions for node (https://nodejs.org/).
Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node/v20.
Additional Details
- Last updated: Wed, 11 Dec 2024 09:35:14 GMT
- Dependencies: undici-types
Credits
These definitions were written by Microsoft TypeScript, Alberto Schiabel, Alvis HT Tang, Andrew Makarov, Benjamin Toueg, Chigozirim C., David Junger, Deividas Bakanas, Eugene Y. Q. Shen, Hannes Magnusson, Huw, Kelvin Jin, Klaus Meinhardt, Lishude, Mariusz Wiktorczyk, Mohsen Azimi, Nikita Galkin, Parambir Singh, Sebastian Silbermann, Thomas den Hollander, Wilco Bakker, wwwy3y3, Samuel Ainsworth, Kyle Uehlein, Thanik Bhongbhibhat, Marcin Kopacz, Trivikram Kamat, Junxiao Shi, Ilia Baryshnikov, ExE Boss, Piotr Błażejewicz, Anna Henningsen, Victor Perin, NodeJS Contributors, Linus Unnebäck, wafuwafu13, Matteo Collina, and Dmitry Semigradsky.