Commands
  • About
  • Software Development
    • Glossary
    • System Design
      • CAP Theorem
      • Data Migration
      • EAV Model
      • ETL Process
      • Event Sourcing
      • Outbox Pattern
      • SOLID Principles
    • Standards
      • Character Encoding
      • Twirp
    • Elixir
      • IEx
      • Mix
      • Libraries
      • Code Snippets
    • Node.js
      • TypeScript
  • Commands
    • Bookmarks
    • AWS
      • Athena
      • ECS
    • Docker
      • Manage Images
      • Manage Containers
    • File System
      • Grep
      • Search Files
      • Miscellaneous
    • Git
      • Setup New Project
      • Manage Remotes
      • Release Project
    • GPG
    • Kafka
      • Quick Start
    • Kubernetes
    • OpenShift
      • Manage Roles and Bindings
    • PostgreSQL
    • SSL / TLS
      • Common OpenSSL Commands
      • Create Self-signed Certificate
      • Java Truststore and Keystore
    • SSH
      • Key Management
      • Port Forwarding
    • YUM
      • List Repositories
      • List Packages
    • Miscellaneous
      • Network
Powered by GitBook
On this page
  • Setup TypeScript, Eslint, Jest and Prettier
  • Narrowing
  • Declaration Merging
  1. Software Development
  2. Node.js

TypeScript

PreviousNode.jsNextBookmarks

Last updated 3 years ago

Setup TypeScript, Eslint, Jest and Prettier

Refer to .

Narrowing

A variable can be declared to have multiple types. TypeScript will throw an error if the variable type might not fit an operation.

function repeat(n: string | number) {
  return "x".repeat(n);
  // [Error]
  // Argument of type 'string | number' is not assignable to parameter of type 'number'.
}

To eliminate the error, we can "narrow" down the type before the operation.

function repeat(x: string | number) {
  if (typeof n === "number") {
    // TypeScript will understand that `n` is a number.
    return "x".repeat(n);
  }

  // TypeScript will understand that `n` is a string.
  return "x".repeat(Number(n));
}

Typescript supports the following narrowing guards:

  • Truthiness

  • Equality

  • typeof operator

  • in operator

  • instanceof operator

Declaration Merging

Declaration merging is an important technique to add custom fields to the existing interfaces.

Assume you are working with the HTTP server. If you want to add the logger to the req object as req.log, TypeScript will warn you that

Property 'log' does not exist on type 'IncomingMessage'.

To achieve that, you have to declare your own IncomingMessage interface to merge with the default http.IncomingMessage interface.

  1. Declare your custom interface - ./src/types/http/index.d.ts

    import { Logger } from "pino";
    
    declare module "http" {
      interface IncomingMessage {
        log: Logger;
      }
    }
  2. In tsconfig.json, update compilerOptions.typeRoots to include the custom "types" folder.

    {
      // ...
      "compilerOptions": {
        // ...
        "typeRoots": ["./node_modules/@types", "./src/types"]
      }
    }
  3. Done. TypeScript will acknowledge that req.log is a Logger from now on.

Please refer to for the details.

The folder path must be /path/to/<MODULE>/index.d.ts for .

here
the official documentation
Official Documentation
ts-node to work properly