Skip to content

Instantly share code, notes, and snippets.

@SuperOleg39
Last active January 3, 2019 13:36
Show Gist options
  • Select an option

  • Save SuperOleg39/82e890ed2c4221adedd0c1ade43a8a71 to your computer and use it in GitHub Desktop.

Select an option

Save SuperOleg39/82e890ed2c4221adedd0c1ade43a8a71 to your computer and use it in GitHub Desktop.
const types = {
obj: 'Object',
arr: 'Array',
str: 'String',
num: 'Number',
func: 'Function',
bool: 'Boolean',
nil: 'Null',
undef: 'Undefined',
date: 'Date',
reg: 'RegExp',
};
export function getType(value: any): string {
return Object.prototype.toString.call(value).slice(8, -1);
}
export function isObject<P = {}>(value: any): value is P {
return getType(value) === types.obj;
}
export function isArray<P = any>(value: any): value is P[] {
return getType(value) === types.arr;
}
export function isArrayClassic<P = any>(value: any): value is P[] {
return Array.isArray(value);
}
export function isString(value: any): value is string {
return getType(value) === types.str;
}
export function isStringClassic(value: any): value is string {
return typeof value === 'string';
}
export function isNumber(value: any): value is number {
return getType(value) === types.num;
}
export function isNumberClassic(value: any): value is number {
return typeof value === 'number';
}
export function isFunction(value: any): value is Function {
return getType(value) === types.func;
}
export function isFunctionClassic(value: any): value is Function {
return typeof value === 'function';
}
export function isBoolean(value: any): value is boolean {
return getType(value) === types.bool;
}
export function isBooleanClassic(value: any): value is boolean {
return typeof value === 'boolean';
}
export function isNull(value: any): value is null {
return getType(value) === types.nil;
}
export function isNullClassic(value: any): value is null {
return value === null;
}
export function isUndefined(value: any): value is undefined {
return getType(value) === types.undef;
}
export function isUndefinedClassic(value: any): value is undefined {
return typeof value === 'undefined';
}
export function isNil(value: any): value is null | undefined {
return value == null;
}
export function isRegExp(value: any): value is RegExp {
return getType(value) === types.reg;
}
export function isDate(value: any): value is Date {
return getType(value) === types.date;
}
@SuperOleg39
Copy link
Copy Markdown
Author

todo: Map, Set, экземпляр класса, NaN(?), Finite(?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment