Last active
May 20, 2021 09:25
-
-
Save sklinov/20263b3d055ca1bb023c63e35dc1ae93 to your computer and use it in GitHub Desktop.
isType typeguard
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
/** | |
* | |
* @param {any} item item you want to typecheck | |
* @param {(keyof T)[]} keys keys that should be present in the type and not be undefined | |
* @returns {boolean} Later in code item is of T type on success | |
*/ | |
export const isType = <T extends unknown> (item: unknown, keys: (keyof T)[]):item is T => { | |
return item !== null && keys.every(key => (item as T)[key as keyof T] !== undefined) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment