Have ideas to improve npm?Join in the discussion! »

    readonly-types

    2.0.0 • Public • Published

    Readonly TypeScript Types

    Build Status type-coverage Known Vulnerabilities npm

    dependencies Status devDependencies Status

    A collection of readonly TypeScript types inspired by the built-in ReadonlyArray, ReadonlyMap, etc.

    Installation

    # yarn 
    yarn add readonly-types
     
    # npm 
    npm install readonly-types

    Usage

    // Here's an example using ReadonlyURL.
    import { ReadonlyURL } from "readonly-types";
     
    // This is fine.
    const hasFooSearchParam = (url: ReadonlyURL) => url.searchParams.has("foo");
     
    // But this won't compile.
    const setFooSearchParam = (url: ReadonlyURL) => url.searchParams.set("foo", "bar");

    The Types

    Linting

    You can ban the mutable counterparts to these readonly types using ESLint, no-restricted-globals and typescript-eslint/ban-types.

      rules: {
        "@typescript-eslint/ban-types": [
          "error",
          {
            types: {
              Record: {
                fixWith: "ReadonlyRecord",
              },
              URL: {
                fixWith: "ReadonlyURL",
              },
              URLSearchParams: {
                fixWith: "ReadonlyURLSearchParams",
              },
              Date: {
                fixWith: "ReadonlyDate",
              },
            },
          },
        ],
        "no-restricted-globals": [
          "error",
          { name: "URL" },
          { name: "URLSearchParams" },
          { name: "Date" },
        ],
      },

    See Also

    Keywords

    none

    Install

    npm i readonly-types

    DownloadsWeekly Downloads

    23

    Version

    2.0.0

    License

    MIT

    Unpacked Size

    25.4 kB

    Total Files

    19

    Last publish

    Collaborators

    • avatar