Skip to content

Repository files navigation

type

An enhanced typeof function. Reliably returns object types, and element types for modern browsers.

import{type}from"@elucidata/type"type(null)// => "null"type(window.undefinedProperty)// => "undefined"type("test")// => "string"type(newString())// => "string"type(1)// => "number"type(newNumber())// => "number"type(true)// => "boolean"type(newBoolean())// => "boolean"type(/test/)// => "regexp"type(newRegExp())// => "regexp"type(newDate())// => "date"type(["test"])// => "array"type(newArray())// => "array"type({test: "test"})// => "object"type(newObject())// => "object"type(document)// => "document"type(document.querySelectorAll("*"))// => "nodelist"type(document.body)// => "bodyelement"type(document.createElement("div"))// => "divelement"type(Symbol("test"))// => "symbol"

Includes helper methods for each type that work as type guards if used in TypeScript:

import*astypefrom"@elucidata/type"type.isObject(target)type.isNotObject(target)type.isBoolean(target)type.isNotBoolean(target)type.isNumber(target)type.isNotNumber(target)type.isString(target)type.isNotString(target)type.isFunction(target)type.isNotFunction(target)type.isArray(target)type.isNotArray(target)type.isDate(target)type.isNotDate(target)type.isRegExp(target)type.isNotRegExp(target)type.isUndefined(target)type.isNotUndefined(target)type.isNull(target)type.isNotNull(target)type.isNodeList(target)type.isNotNodeList(target)type.isSymbol(target)type.isNotSymbol(target)type.isElement(target)type.isNotElement(target)// is[Not]Element() deals with HTMLElementstype.isElement(document.body)// => truetype.isElement({})// => false

Installation

Pick your poison:

npm install @elucidata/type

Definitions

exportdeclareconstgetType: (target: any)=>stringexportdeclareconstof: (target: any)=>stringexportdeclareconsttype: (target: any)=>stringexportdeclareconstisBoolean: (target: any)=>target is booleanexportdeclareconstisNotBoolean: (target: any)=>target is unknownexportdeclareconstisNumber: (target: any)=>target is numberexportdeclareconstisNotNumber: (target: any)=>target is unknownexportdeclareconstisString: (target: any)=>target is stringexportdeclareconstisNotString: (target: any)=>target is unknownexportdeclareconstisFunction: (target: any)=>target is FunctionexportdeclareconstisNotFunction: (target: any)=>target is unknownexportdeclareconstisArray: (target: any)=>target is any[]exportdeclareconstisNotArray: (target: any)=>target is unknownexportdeclareconstisDate: (target: any)=>target is DateexportdeclareconstisNotDate: (target: any)=>target is unknownexportdeclareconstisRegExp: (target: any)=>target is RegExpexportdeclareconstisNotRegExp: (target: any)=>target is unknownexportdeclareconstisUndefined: (target: any)=>target is undefinedexportdeclareconstisNotUndefined: (target: any)=>target is unknownexportdeclareconstisNull: (target: any)=>target is nullexportdeclareconstisNotNull: (target: any)=>target is unknownexportdeclareconstisNodeList: (target: any)=>target is NodeListexportdeclareconstisNotNodeList: (target: any)=>target is unknownexportdeclareconstisObject: (target: any)=>target is ObjectexportdeclareconstisNotObject: (target: any)=>target is unknownexportdeclareconstisSymbol: (target: any)=>target is SymbolexportdeclareconstisNotSymbol: (target: any)=>target is unknownexportdeclareconstisEmpty: (target: any)=>booleanexportdeclareconstisNotEmpty: (target: any)=>booleanexportdeclareconstisElement: (target: any)=>target is HTMLElementexportdeclareconstisNotElement: (target: any)=>target is unknown

About

An enhanced typeof function. Reliably returns object types, and element types for modern browsers.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages