Type safe variable checks
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.
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
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
}
};