Skip to content

Instantly share code, notes, and snippets.

@danielearwicker
Last active August 2, 2016 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielearwicker/f415f68e15b71a9c518da65b2bc11363 to your computer and use it in GitHub Desktop.
Save danielearwicker/f415f68e15b71a9c518da65b2bc11363 to your computer and use it in GitHub Desktop.
interface Person {
readonly name?: string;
readonly age?: number;
}
const x = record<Person>({ name: "Bart" }).set({ age: 10 });
const y = x.set({ age: 11 });
/*
* Defines the set method available on any record.
*/
export interface Record<Fields> {
set(fields: Fields): Record<Fields> & Fields;
}
/*
* Creates a new record with the specified initial field
* values, with type that combines the fields with the set
* method.
*/
export function record<Fields>(init: Fields) {
return prototype.set(init) as Record<Fields> & Fields;
}
/*
* Implementation of set method. Nothing fancy.
*/
const prototype = {
set(fields: any) {
var clone: any = Object.create(prototype);
if (this) {
for (var key of Object.keys(this)) {
clone[key] = this[key];
}
}
for (var key of Object.keys(fields)) {
clone[key] = fields[key];
}
return clone;
}
}
@danielearwicker
Copy link
Author

danielearwicker commented Aug 2, 2016

The most minimal form of immutable record.

The problem it has is that in TS 2.0 with strict null checks, the fields of Person being optional, they can't be directly used as they might be undefined.

Also the clone operation is very expensive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment