ts-node

website·Indexed 36 days ago

https://typestrong.org/ts-node/

TypeScript execution engine for Node.js that allows running TypeScript files directly. Features include a TypeScript REPL, support for CommonJS and native ECMAScript modules, built-in SWC support via the --swc flag, and a comprehensive API for compiling TypeScript to JavaScript.

Tokens
17.1K
Snippets
121
Records
165
Agent score
72%

What's inside ts-node

  1. Overview of ts-node TypeScript execution engine

    ts-node is a TypeScript execution engine and REPL for Node.js. It JIT (Just-In-Time) transforms TypeScript into JavaScript by hooking into Node's module loading APIs, allowing developers to execute TypeScript files directly without a manual precompilation step. It is designed to work seamlessly with other Node.js tools, libraries, test runners, debuggers, and CLI tools.
  2. Overview of TSCommon compatibility layer

    The TSCommon interface provides a set of common TypeScript interfaces and API declarations used by ts-node. This layer allows ts-node to maintain compatibility between different TypeScript versions. Specifically, it explicitly declares public APIs from typescript@next that may not yet be available in typescript@latest, ensuring that ts-node can utilize newer features while remaining stable.
  3. Use the TSError class for TypeScript diagnostics errors

    The TSError class extends BaseError and is used to represent errors originating from TypeScript diagnostics. It provides detailed information about the diagnostic codes and the associated diagnostic objects.
    // Example instantiation of TSError
    const error = new TSError(
      "Unexpected token", 
      [123], 
      [diagnosticObject]
    );
  4. Use the TypeInfo interface for type information

    The TypeInfo interface provides information retrieved from a type info check. It contains basic metadata about the type, specifically the name of the type and any associated comments.
    interface TypeInfo {
      comment: string;
      name: string;
    }
  5. Understand ts-node file transformation scoping

    ts-node uses a mechanism called "scoping" to decide which files to transform from TypeScript to JavaScript and which to ignore. Note that ignoring a file only prevents transformation; it does not prevent Node.js from attempting to execute the file. If a file requires transformation but is ignored, Node.js may throw syntax errors because it cannot parse TypeScript type syntax or unsupported ECMAScript features.
  6. Understand how ts-node executes TypeScript code

    ts-node enables the execution of TypeScript by registering hooks for .ts, .tsx, .js, and .jsx extensions. When Node.js attempts to load these files, ts-node intercepts the process, transforms the TypeScript code into JavaScript based on the settings in the project's tsconfig.json (mimicking tsc compilation), and passes the resulting JavaScript to the Node.js engine for execution. Additionally, ts-node registers hooks to handle sourcemap application for stack traces and to remap .js imports back to their corresponding .ts source files.
  7. Install optional dependencies for ts-node

    Depending on your project configuration, you may need to install tslib and Node.js type definitions (@types/node) as development dependencies.
    npm install -D tslib @types/node
  8. Implement a third-party transpiler for ts-node

    To implement a third-party transpiler for ts-node, create a CommonJS module that provides a named export called create. This export must be of type TranspilerModule, which contains a create property that returns a TranspilerFactory.
    // Example of a TranspilerModule implementation
    export const create = () => { 
      // Return a TranspilerFactory implementation
    };
  9. Develop a custom ts-node transpiler plugin

    To create a custom transpiler plugin, develop a Node.js module (local script or npm package) that exports a create function conforming to the TranspilerModule interface. ts-node invokes this create function at startup to instantiate one or more transpiler instances responsible for transforming TypeScript into JavaScript.
  10. Configure ts-node in a script shebang

    To use ts-node as the interpreter for a script, use the #!/usr/bin/env ts-node shebang. For maximum portability, define options in tsconfig.json rather than the shebang itself. If you must include options in the shebang, use the env -S flag (supported on recent versions of env on Mac and Linux).
    #!/usr/bin/env ts-node
    
    // Options are read from tsconfig.json
    console.log("Hello, world!");
    #!/usr/bin/env -S ts-node --files
    
    // This shebang works on Mac and Linux with newer versions of env