Skip to content

Instantly share code, notes, and snippets.

@ahtcx
Created December 24, 2020 16:31
Show Gist options
  • Save ahtcx/5bc1f3a247b0953039f307381a3c655e to your computer and use it in GitHub Desktop.
Save ahtcx/5bc1f3a247b0953039f307381a3c655e to your computer and use it in GitHub Desktop.
const getTypeofValue = (value: any) => typeof value;
export type TypeofReturnType = ReturnType<typeof getTypeofValue>;
// prettier-ignore
export type TypeFromTypeString<TypeString extends TypeofReturnType> =
TypeString extends "string" ? string :
TypeString extends "number" ? number :
TypeString extends "bigint" ? BigInt :
TypeString extends "boolean" ? boolean :
TypeString extends "symbol" ? symbol :
TypeString extends "undefined" ? undefined :
TypeString extends "object" ? object :
TypeString extends "function" ? Function :
never
export const isTypeof = <TypeString extends TypeofReturnType>(type: TypeString) => (
value: any
): value is TypeFromTypeString<TypeString> => typeof value === type;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment