Created
November 4, 2020 23:18
-
-
Save bwestergard/3ff808129fb647d48a89093532245ee2 to your computer and use it in GitHub Desktop.
Expanding Record
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
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