typed-immutable-methods
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

typed-immutable-methods

Immutable getter/setter methods with Types

npm

Installation

$ npm install typed-immutable-methods --save 

Usage

import { get } from 'typed-immutable-methods'
 
const entity = {
  a: {
    b: {
      c: 'string value',
      d: 23,
    }
  }
}
 
const c = get(entity, ['a', 'b', 'c']) // c is string type by type inference
const d = get(entity, ['a', 'b', 'd']) // d is number type by type inference
const b = get(entity, ['a', 'b'])      // b is { c: string; d: number } by type inference
 
expect(b).not.toEqual(entity.a.b)      // different references
 
get(entity, ['d'])                     // Compile error
import { set } from 'typed-immutable-methods'
 
const updated = set(entity, ['a', 'b', 'd'], 24)
 
expect(updated).toEqual({
  a: {
    b: {
      c: 'string value',
      d: 24,
    }
  }
})
expect(updated).not.toEqual(entity)          // different references
 
set(entity, ['a', 'b', 'd'], 'not a number') // Compile error

Readme

Keywords

none

Package Sidebar

Install

npm i typed-immutable-methods

Weekly Downloads

0

Version

1.0.3

License

MIT

Last publish

Collaborators

  • isoden