Classic

The Classic module resolution strategy mimics the behavior of the original TypeScript compiler. This approach is simplistic and mainly intended for backwards compatibility with older TypeScript projects. It follows these steps:

  • Check for a file with the specified module name.
  • Check for a .ts or .d.ts file with the specified module name.

Syntax:

import { MyClass } from "./MyClass"; // Looks for MyClass.ts or MyClass.d.ts

Example: This example demonstrates the Classic module resolution strategy. We import a class from a file located in the same directory.

JavaScript
// index.ts
import { MyClass } from "./MyClass";

const myClass = new MyClass();
console.log(myClass.sayHello());
JavaScript
// MyClass.ts
export class MyClass {
  sayHello() {
    return "Hello from MyClass!";
  }
}

Output:

"Hello from MyClass!"

Module Resolution Techniques & Strategies in TypeScript

TypeScript, a typed superset of JavaScript, provides enhanced tooling and type safety for modern JavaScript development. A critical aspect of TypeScript is its module resolution mechanism, which determines how to locate and import modules. Understanding TypeScript’s module resolution techniques and strategies is essential for managing dependencies and structuring code in large applications effectively.

Syntax:

import { MyModule } from "./myModule";
import * as MyModule from "./myModule";
import MyDefaultModule from "./myDefaultModule";

Similar Reads

Classic

The Classic module resolution strategy mimics the behavior of the original TypeScript compiler. This approach is simplistic and mainly intended for backwards compatibility with older TypeScript projects. It follows these steps:...

Node

The Node module resolution strategy is based on the Node.js resolution algorithm. This approach is more sophisticated and is the default in TypeScript. It follows these steps:...

Steps to Create Application

Step 1: Initialize a TypeScript Project....