1.0.0 • Published 6 years ago

@typeforce/immutable-dts v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

immutable-dts

NPM NPM LICENSE

Typescript type definitions for treating objects as immutable.

Usage

    npm install @typeforce/immutable-dts --save   # Install package via NPM
    import { Immutable } from "@typeforce/immutable-dts";

    type Account = {
        name: string,
        age: number,
        obj: { x: number, y: number };
    };

    // An easy way of making an object's properties read-only is using the
    // Typescript `Readonly` type.
    const john: Readonly<Account> = {
        name: "John Doe",
        age: 42,
        obj: {
            x: 5,
            y: 3
        }
    };

    // However, `Readonly` only protects properties that are immediate children
    // of their object; it is not recursive.
    john.obj.x = 6; // <- This should not be possible! We made `john` readonly!

    // `Immutable` recursively declares object properties as read-only, so the
    // object cannot be modified.
    const immutableJohn = john as Immutable<Account>;
    account.obj.x = 6; // <- Compiler error: `x` is readonly!

This package contains TypeScript type declarations.

Build

When building the project, a folder named dist/ will be created if it does not already exist, where the compiled code will be outputted to. Type declaration files will also be generated and outputted to the dist/ folder.

    npm install     # Installs dependencies.
    npm run build   # Build the project.

License

Refer to the LICENSE file for license information.