Step 1: Type Definition

Extend the RegExpToken interface

Only required for TypeScript users

To maintain strong typing on custom tokens, you should extend the built-in RegExpToken interface with the type of your custom token.

// Import the interface and helper types (as needed)
import { RegExpToken, LiteralFunction, GenericFunction, IncompleteToken } from 'readable-regexp';

// Extend the interface with a declaration
declare module 'readable-regexp' {
  interface RegExpToken {

    // ===== CONSTANT tokens =====

    severity: RegExpToken;
    matchAll: RegExpToken;

    // ===== DYNAMIC tokens =====
    // Dynamic tokens must intersect the IncompleteToken type to signify that parameters are required

    // Use the LiteralFunction type for tokens that take a single string parameter
    notExactly: LiteralFunction & IncompleteToken;
    // Use the GenericFunction type for all other dynamic tokens
    // Use a union for function overloading
    exactValue: GenericFunction<[num: number] | [bool: boolean], RegExpToken> & IncompleteToken;

    // ===== MIXED tokens =====

    alpha: GenericFunction<[upper: boolean], RegExpToken> & RegExpToken;

  }
}

Last updated