Created
March 3, 2023 16:59
-
-
Save timmattison/0ddd6b386eece4633424a5db9a0d0a09 to your computer and use it in GitHub Desktop.
fromPartial implementation, attempt #1
This file contains 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
import {PartialDeep} from "type-fest"; | |
function fromPartial<T extends {}>(mock: PartialDeep<T>) { | |
const proxy = new Proxy(mock, { | |
get(target, p, receiver) { | |
// If the receiver (the original type) doesn't have it then we ignore it | |
if (!(p in receiver)) return undefined | |
if (!(p in target)) { | |
throw new Error(`${String(p)} not found in mocked object`); | |
} | |
return Reflect.get(target, p, receiver); | |
}, | |
}) | |
return proxy as T; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment