Created
December 24, 2020 16:31
-
-
Save ahtcx/5bc1f3a247b0953039f307381a3c655e to your computer and use it in GitHub Desktop.
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
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