Skip to content

Instantly share code, notes, and snippets.

@bwestergard
Created November 4, 2020 23:18
Show Gist options
  • Save bwestergard/3ff808129fb647d48a89093532245ee2 to your computer and use it in GitHub Desktop.
Save bwestergard/3ff808129fb647d48a89093532245ee2 to your computer and use it in GitHub Desktop.
Expanding Record
const foo = {foo: 1}
const bar = {bar: 2}
const baz = {...foo, ...bar}
const merge = <X,Y>(a:X, b:Y): X&Y => ({...a, ...b})
class ExpandingRecord<A> {
record: A;
constructor(record: A) {
this.record = record;
}
merge<B>(b: B): ExpandingRecord<A & B> {
return new ExpandingRecord(merge(this.record, b))
}
result() {
return this.record
}
}
const initRec = new ExpandingRecord({bar: 1})
const other = {foo:2}
const second : {foo: number, blah: number} = initRec.merge({foo:2}).merge({blah:3}).result()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment