Last active
August 2, 2016 08:03
-
-
Save danielearwicker/f415f68e15b71a9c518da65b2bc11363 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Person { | |
readonly name?: string; | |
readonly age?: number; | |
} | |
const x = record<Person>({ name: "Bart" }).set({ age: 10 }); | |
const y = x.set({ age: 11 }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 beundefined
.Also the clone operation is very expensive.