Skip to content

Instantly share code, notes, and snippets.

@sklinov
Last active May 20, 2021 09:25
Show Gist options
  • Save sklinov/20263b3d055ca1bb023c63e35dc1ae93 to your computer and use it in GitHub Desktop.
Save sklinov/20263b3d055ca1bb023c63e35dc1ae93 to your computer and use it in GitHub Desktop.
isType typeguard
/**
*
* @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