Last active
July 15, 2023 17:07
-
-
Save Merott/c89936400ac56bb92ff4a594b37ec072 to your computer and use it in GitHub Desktop.
A TypeScript guard for object types, with an optional parameter to verify the shape of that object, specifying the properties and their corresponding types.
This file contains hidden or 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
type BasicTypeMap = { | |
/* These are the types you might get with the `typeof` operator. */ | |
'string': string, | |
'number': number, | |
'bigint': bigint, | |
'boolean': boolean, | |
'symbol': symbol, | |
'undefined': undefined, | |
'object': object, | |
'function': (...args: unknown[]) => unknown, | |
} | |
type BasicTypeName = keyof BasicTypeMap; | |
type BasicType<Name extends BasicTypeName> = BasicTypeMap[Name]; | |
export function isObject(value: unknown): value is object | |
export function isObject<TValue, TProps extends Record<string, BasicTypeName>>( | |
value: TValue, | |
props: TProps, | |
): value is (object extends TValue ? object : TValue) & { | |
[PropKey in keyof TProps]: BasicType<TProps[PropKey]> | |
} | |
export function isObject<TProps extends Record<string, BasicTypeName>>( | |
value: unknown, | |
props?: TProps, | |
) { | |
if (typeof value !== 'object' || value === null) return false | |
if (typeof props === 'object') { | |
for (const propKey in props) { | |
const propValue = (value as Record<string, unknown>)[propKey] | |
if (typeof propValue !== props[propKey]) { | |
return false | |
} | |
} | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: