Created
January 16, 2025 15:26
-
-
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
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
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