I have a schema defined
type Schema = {
a: { a: 1 }
b: { b: 2 }
}
And I want a function to create objects that conform to multiple schemas.
function createObject<K extends keyof Schema>(schema: Array<K>, obj: Schema[K]) {}
createObject(["a"], { a: 1 }) // works
createObject(["b"], { b: 2 }) // works
createObject(["a", "b"], { b: 2 }) // doesn't error but it should
createObject(["a", "b"], { a: 1, b: 2 }) // works
I've tried a few other things. Interestingly when you &
a union with itself, it does a distributes the &
across all items in the union and doesn't quite get me what I want. I want some to operate on {a: 1} | {b: 2}
to get {a: 1, b: 2}
. Any ideas?