Skip to content

Instantly share code, notes, and snippets.

@timmattison
Created March 3, 2023 16:59
Show Gist options
  • Save timmattison/0ddd6b386eece4633424a5db9a0d0a09 to your computer and use it in GitHub Desktop.
Save timmattison/0ddd6b386eece4633424a5db9a0d0a09 to your computer and use it in GitHub Desktop.
fromPartial implementation, attempt #1
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