@jakesidsmith/tis
v1.0.0
Published
Type safe variable checks
Downloads
4
Maintainers
Readme
'tis
Type safe variable checks
Install
npm i @jakesidsmith/tis -P
-P
is shorthand for --save-production
and will add this to your package dependencies
.
Use -D
(shorthand for --save-dev
) if you are only using this for testing.
Available utilities
isNumber
: Returns true when the input is a numberisString
: Returns true when the input is a stringisBoolean
: Returns true when the input is a booleanisUndefined
: Returns true when the input is undefinedisDefined
: Returns true when the input is not undefinedisNull
: Returns true when the input is nullisNullish
: Returns true when the input is null or undefinedisArray
: Returns true when the input is an arrayisObject
: Returns true when the input is an object (includes arrays, excludes null)isFunction
: Returns true when the input is a function or class constructor
Example usage
import { isObject, isArray, isString } from '@jakesidsmith/tis';
const example = (
input: string | number | readonly string[] | Record<string, string>
) => {
if (isObject(input)) {
if (isArray(input)) {
// Guaranteed to be a readonly string[]
} else {
// Guaranteed to be a Record<string, string>
}
} else if (isString(input)) {
// Guaranteed to be a string
} else {
// Guaranteed to be a number
}
};