nightshirt
nightshirt lets you define strongly-typed Immutable.js Records in TypeScript.
nightshirt requires TypeScript 2.1, as it uses keyof
and lookup types.
Installation
nightshirt is available via NPM:
npm install --save nightshirt
Usage
Immutable.js provides a Record
function for creating record classes, and nightshirt works similarly. Define an interface with your record's properties, and then call makeRecordFactory<T>
to create your record class.
Records are of type Record<T>
, which makes all properties on T
into readonly
versions of those properties, and mixes in the methods available on Immutable.js records: get
, set
, merge
, etc.
import makeRecordFactory, {Record} from 'nightshirt'
interface Contact {
name: string
email: string
private: boolean
}
const Contact = makeRecordFactory<Contact>({
name: '',
email: '',
private: false
})
let c: Record<Contact> = new Contact()
c = c.set('private', true)
// c.private = false // error: 'private' is a read-only property
// c = c.set('private', 2) // error: `2` is not a boolean (Contact['private'])
// c = c.set('phone', '...') // error: `'phone'` is not 'name' | 'email' | 'private'
c = c.withMutations((c) => {
c.name = 'Bugs Bunny'
e.email = 'bugs@example.com'
})
c = c.merge({
email: 'bugs.bunny@example.com',
private: false
})