require Keyword in Typescript

Do you want to know about the Typescript ‘require’ keyword? In this Typescript, I will explain to you how to use the require keyword in Typescript with various examples.

The ‘require’ keyword in TypeScript is used for importing modules, JSON files, and scripts, following the CommonJS syntax. While it’s essential in Node.js environments, TypeScript also supports ES6 import syntax. Utilizing require effectively involves understanding its syntax, applying type declarations for better type safety, and recognizing its use cases in various TypeScript applications, especially those built on Node.js.

What is require keyword in TypeScript?

The require keyword in TypeScript is used to import modules, JSON files, and other scripts into a TypeScript file. It’s similar to the import statement but follows the CommonJS module syntax, which is native to Node.js environments.

Examples of require Keyword in Typescript

Here are a few examples of how to use the ‘require’ keyword in Typescript.

Example 1: Importing a Custom Module

Let’s start with a basic example where we create a custom module and import it using require. First, you can create a custom module like the one below:

mathOperations.ts

export function add(a: number, b: number): number {
    return a + b;
}

export function subtract(a: number, b: number): number {
    return a - b;
}

app.ts

Now, you can import and use the module below:

const mathOperations = require('./mathOperations');

console.log(mathOperations.add(5, 3));  // Output: 8
console.log(mathOperations.subtract(10, 6));  // Output: 4

Example 2: Importing a JSON File

TypeScript also allows importing JSON files directly. This can be handy for configuration or data handling.

First, you can create a json file like the one below:

config.json

{
    "appName": "My TypeScript App",
    "version": "1.0.0"
}

app.ts

Then you can import and use the json file like the below code:

const config = require('./config.json');

console.log(`App Name: ${config.appName}, Version: ${config.version}`);

Example 3: Importing Node.js Core Modules

Node.js core modules, like fs for file system operations, can be imported using require.

app.ts

const fs = require('fs');

fs.readFile('config.json', 'utf8', (err, data) => {
    if (err) throw err;
    console.log(data);
});

Here are some best practices you can follow while using the require keyword in Typescript.

  1. Type Declarations: When using require, TypeScript might not know the types being imported. It’s a good practice to declare types either through TypeScript declaration files (.d.ts) or by using DefinitelyTyped (@types/ packages).
  2. ES6 Imports: If possible, prefer using ES6 import syntax as it’s more declarative and supports tree-shaking (eliminating unused exports).

Conclusion

The require keyword in TypeScript is helpful for module importation, especially in Node.js environments. In this Typescript tutorial, I have explained how to use the require keyword in Typescript, especially for importing external modules, JSON files, and core Node.js functionality into their TypeScript applications.

You may also like:

>