typed-immutable-methods
v1.0.3
Published
Immutable getter/setter methods with Types
Downloads
3
Readme
typed-immutable-methods
Immutable getter/setter methods with Types
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