Skip to content

Instantly share code, notes, and snippets.

@akameco
Created April 18, 2017 05:19
Show Gist options
  • Save akameco/184189c564b992842dd0ac296dbc91f7 to your computer and use it in GitHub Desktop.
Save akameco/184189c564b992842dd0ac296dbc91f7 to your computer and use it in GitHub Desktop.
// @flow
import * as I from 'immutable'
export function defineRecord<T: Object>(
name: string,
spec: T
): (init: $Shape<T>) => Record<T> {
return I.Record(spec, name)
}
export type Record<T: Object> = RecordMethods<T> & T;
declare class RecordMethods<T: Object> {
get<A>(key: $Keys<T>): A;
set<A>(key: $Keys<T>, value: A): Record<T>;
update<A>(key: $Keys<T>, updater: (value: A) => A): Record<T>;
updateIn<A>(path: Iterable<any>, notSetOrUpdater: A | (value: A) => A, updater?: (value: A) => A): Record<T>;
setIn<A>(path: Iterable<any>, value: A): Record<T>;
deleteIn<A>(path: Iterable<any>): Record<T>;
merge(values: $Shape<T>): Record<T>;
inspect(): string;
toObject(): T;
// add more as needed
}
type X = {
id: number,
name: string,
}
type XRecord = Record<X>
const Thing = defineRecord('Thing', ({id: 0, name: ''}: X))
const x: XRecord = Thing({name: 'akameco', id: 0})
x.get('name')
x.set('name', 'hoge')
x.setIn('name', 'hoge')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment