JSPM

  • Created
  • Published
  • Downloads 349
  • Score
    100M100P100Q84334F
  • License MIT

A TypeScript type guard library

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (guardz) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    guardx

    A simple and lightweight TypeScript type guard library.

    Installation

    npm install guardx
    # or
    yarn add guardx

    Usage

    import { isString, isNumber, isObjectWithKeys } from 'guardx';
    
    const data: unknown = getDataFromSomewhere();
    
    if (isString(data)) {
      // data is now typed as string
      console.log(data.toUpperCase());
    }
    
    if (isObjectWithKeys(data, ['name', 'age'])) {
      // data is now typed as { name: unknown; age: unknown; } & Record<string | number | symbol, unknown>
      console.log(`Name: ${data.name}`);
      if (isNumber(data.age)) {
        // data.age is now typed as number
        console.log(`Age: ${data.age}`);
      }
    }
    
    // Add more examples of your specific guards here

    Available Guards

    • isString(value: unknown): value is string
    • isNumber(value: unknown): value is number
    • isBoolean(value: unknown): value is boolean
    • // ... List other guards here

    Contributing

    Contributions are welcome! Please open an issue or submit a pull request.

    License

    MIT