Skip to content

Instantly share code, notes, and snippets.

@colelawrence
Created January 16, 2025 15:26
Show Gist options
  • Save colelawrence/724aefe95f5ee1fd726277307848d1b9 to your computer and use it in GitHub Desktop.
Save colelawrence/724aefe95f5ee1fd726277307848d1b9 to your computer and use it in GitHub Desktop.
Simple object predicate macro - fast check for an object you need to check many keys exist on
const objectPredicateMacro = <T>(keys: (keyof T | (string & {}))[]): ((obj: unknown) => obj is T) => {
return new Function(
"isObject",
"value",
`return isObject(value) ${keys.map((key) => `&& ${JSON.stringify(key)} in value`).join(" ")};`,
).bind(null, isObject);
};
const isStore = objectPredicateMacro<JotaiStore>(["get", "set", "sub", "unstable_derive"]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment