2021-01-02 17:25:04 +13:00
# is
2017-09-22 00:44:27 +07:00
2019-02-12 14:40:44 +07:00
> Type check values
For example, `is.string('🦄') //=> true`
2017-09-22 00:44:27 +07:00
< img src = "header.gif" width = "182" align = "right" >
2019-02-12 14:40:44 +07:00
## Highlights
- Written in TypeScript
- [Extensive use of type guards ](#type-guards )
2020-01-22 12:08:35 +01:00
- [Supports type assertions ](#type-assertions )
2020-01-29 17:22:35 +01:00
- [Aware of generic type parameters ](#generic-type-parameters ) (use with caution)
2019-02-12 14:40:44 +07:00
- Actively maintained
2020-06-27 20:13:23 +09:00
- 
2019-02-12 14:40:44 +07:00
2017-09-22 00:44:27 +07:00
## Install
2022-02-01 11:37:28 +07:00
```sh
npm install @sindresorhus/is
2017-09-22 00:44:27 +07:00
```
## Usage
```js
2022-06-11 17:44:01 +07:00
import is from '@sindresorhus/is ';
2017-09-22 00:44:27 +07:00
is('🦄');
//=> 'string'
is(new Map());
//=> 'Map'
is.number(6);
//=> true
```
2020-01-22 18:47:01 +07:00
[Assertions ](#type-assertions ) perform the same type checks, but throw an error if the type does not match.
2020-01-22 12:08:35 +01:00
```js
2022-06-11 17:44:01 +07:00
import {assert} from '@sindresorhus/is ';
2020-01-22 12:08:35 +01:00
2020-01-22 18:47:01 +07:00
assert.string(2);
//=> Error: Expected value which is `string` , received value of type `number` .
2020-01-22 12:08:35 +01:00
```
2024-04-23 07:58:28 +02:00
Assertions (except `assertAll` and `assertAny` ) also support an optional custom error message.
```js
import {assert} from '@sindresorhus/is ';
assert.nonEmptyString(process.env.API_URL, 'The API_URL environment variable is required.');
//=> Error: The API_URL environment variable is required.
```
2020-01-22 18:47:01 +07:00
And with TypeScript:
```ts
import {assert} from '@sindresorhus/is ';
assert.string(foo);
// `foo` is now typed as a `string` .
```
2017-09-22 00:44:27 +07:00
2023-08-07 08:50:03 +08:00
### Named exports
Named exports allow tooling to perform tree-shaking, potentially reducing bundle size by including only code from the methods that are used.
Every method listed below is available as a named export. Each method is prefixed by either `is` or `assert` depending on usage.
For example:
```js
import {assertNull, isUndefined} from '@sindresorhus/is ';
```
2017-09-22 00:44:27 +07:00
## API
### is(value)
Returns the type of `value` .
Primitives are lowercase and object types are camelcase.
Example:
- `'undefined'`
- `'null'`
- `'string'`
- `'symbol'`
- `'Array'`
- `'Function'`
- `'Object'`
2023-08-07 08:50:03 +08:00
This method is also exported as `detect` . You can import it like this:
```js
import {detect} from '@sindresorhus/is ';
```
2018-03-30 09:25:09 +05:30
Note: It will throw an error if you try to feed it object-wrapped primitives, as that's a bad practice. For example `new String('foo')` .
2017-09-22 00:44:27 +07:00
### is.{method}
2023-08-07 08:50:03 +08:00
All the below methods accept a value and return a boolean for whether the value is of the desired type.
2017-09-22 00:44:27 +07:00
#### Primitives
##### .undefined(value)
##### .null(value)
2022-02-01 11:37:28 +07:00
2017-09-22 00:44:27 +07:00
##### .string(value)
##### .number(value)
2019-06-30 10:09:07 +02:00
Note: `is.number(NaN)` returns `false` . This intentionally deviates from `typeof` behavior to increase user-friendliness of `is` type checks.
2017-09-22 00:44:27 +07:00
##### .boolean(value)
##### .symbol(value)
2019-05-13 11:04:48 +07:00
##### .bigint(value)
2017-09-22 00:44:27 +07:00
#### Built-in types
2020-07-25 10:11:59 +03:00
##### .array(value, assertion?)
Returns true if `value` is an array and all of its items match the assertion (if provided).
```js
is.array(value); // Validate `value` is an array.
is.array(value, is.number); // Validate `value` is an array and all of its items are numbers.
```
2017-09-22 00:44:27 +07:00
##### .function(value)
2022-02-01 11:37:28 +07:00
2017-09-22 00:44:27 +07:00
##### .buffer(value)
2022-02-27 15:16:25 +08:00
##### .blob(value)
2017-09-22 00:44:27 +07:00
##### .object(value)
Keep in mind that [functions are objects too ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions ).
2018-11-01 13:21:49 +02:00
##### .numericString(value)
2019-06-30 10:09:07 +02:00
Returns `true` for a string that represents a number satisfying `is.number` , for example, `'42'` and `'-8.3'` .
2018-11-01 13:21:49 +02:00
2018-11-02 16:04:44 +00:00
Note: `'NaN'` returns `false` , but `'Infinity'` and `'-Infinity'` return `true` .
2018-11-02 17:53:03 +07:00
2017-09-22 00:44:27 +07:00
##### .regExp(value)
##### .date(value)
##### .error(value)
##### .nativePromise(value)
##### .promise(value)
Returns `true` for any object with a `.then()` and `.catch()` method. Prefer this one over `.nativePromise()` as you usually want to allow userland promise implementations too.
2017-09-28 23:01:48 -04:00
##### .generator(value)
Returns `true` for any object that implements its own `.next()` and `.throw()` methods and has a function definition for `Symbol.iterator` .
##### .generatorFunction(value)
2017-10-15 23:29:10 -04:00
##### .asyncFunction(value)
2017-10-23 19:03:35 +07:00
Returns `true` for any `async` function that can be called with the `await` operator.
2017-10-15 23:29:10 -04:00
```js
is.asyncFunction(async () => {});
2020-01-30 00:38:17 +07:00
//=> true
2017-10-15 23:29:10 -04:00
is.asyncFunction(() => {});
2020-01-30 00:38:17 +07:00
//=> false
2017-10-15 23:29:10 -04:00
```
2020-01-29 18:35:58 +01:00
##### .asyncGenerator(value)
```js
is.asyncGenerator(
(async function * () {
yield 4;
})()
);
2020-01-30 00:38:17 +07:00
//=> true
2020-01-29 18:35:58 +01:00
is.asyncGenerator(
(function * () {
yield 4;
})()
);
2020-01-30 00:38:17 +07:00
//=> false
2020-01-29 18:35:58 +01:00
```
##### .asyncGeneratorFunction(value)
```js
is.asyncGeneratorFunction(async function * () {
yield 4;
});
2020-01-30 00:38:17 +07:00
//=> true
2020-01-29 18:35:58 +01:00
is.asyncGeneratorFunction(function * () {
yield 4;
});
2020-01-30 00:38:17 +07:00
//=> false
2020-01-29 18:35:58 +01:00
```
2017-11-10 19:11:35 +01:00
##### .boundFunction(value)
Returns `true` for any `bound` function.
```js
is.boundFunction(() => {});
2020-01-30 00:38:17 +07:00
//=> true
2017-11-10 19:11:35 +01:00
is.boundFunction(function () {}.bind(null));
2020-01-30 00:38:17 +07:00
//=> true
2017-11-10 19:11:35 +01:00
is.boundFunction(function () {});
2020-01-30 00:38:17 +07:00
//=> false
2017-11-10 19:11:35 +01:00
```
2017-09-22 00:44:27 +07:00
##### .map(value)
##### .set(value)
##### .weakMap(value)
##### .weakSet(value)
2022-06-13 15:05:26 +08:00
##### .weakRef(value)
2017-09-22 00:44:27 +07:00
#### Typed arrays
##### .int8Array(value)
##### .uint8Array(value)
##### .uint8ClampedArray(value)
##### .int16Array(value)
##### .uint16Array(value)
##### .int32Array(value)
##### .uint32Array(value)
##### .float32Array(value)
##### .float64Array(value)
2019-05-13 11:04:48 +07:00
##### .bigInt64Array(value)
##### .bigUint64Array(value)
2017-09-22 00:44:27 +07:00
#### Structured data
##### .arrayBuffer(value)
##### .sharedArrayBuffer(value)
##### .dataView(value)
2022-01-17 00:55:40 -05:00
##### .enumCase(value, enum)
2022-01-18 01:48:03 -03:00
TypeScript-only. Returns `true` if `value` is a member of `enum` .
2022-01-17 00:55:40 -05:00
```ts
enum Direction {
Ascending = 'ascending',
Descending = 'descending'
}
is.enumCase('ascending', Direction);
//=> true
is.enumCase('other', Direction);
//=> false
```
2018-09-28 11:54:35 +05:30
#### Emptiness
##### .emptyString(value)
Returns `true` if the value is a `string` and the `.length` is 0.
2022-02-25 16:19:29 +07:00
##### .emptyStringOrWhitespace(value)
Returns `true` if `is.emptyString(value)` or if it's a `string` that is all whitespace.
2018-09-28 11:54:35 +05:30
##### .nonEmptyString(value)
Returns `true` if the value is a `string` and the `.length` is more than 0.
2022-02-25 09:10:57 +00:00
##### .nonEmptyStringAndNotWhitespace(value)
Returns `true` if the value is a `string` that is not empty and not whitespace.
```js
const values = ['property1', '', null, 'property2', ' ', undefined];
values.filter(is.nonEmptyStringAndNotWhitespace);
//=> ['property1', 'property2']
```
2018-09-28 11:54:35 +05:30
##### .emptyArray(value)
Returns `true` if the value is an `Array` and the `.length` is 0.
##### .nonEmptyArray(value)
Returns `true` if the value is an `Array` and the `.length` is more than 0.
##### .emptyObject(value)
2018-09-28 13:30:19 +07:00
Returns `true` if the value is an `Object` and `Object.keys(value).length` is 0.
2018-09-28 11:54:35 +05:30
Please note that `Object.keys` returns only own enumerable properties. Hence something like this can happen:
```js
const object1 = {};
Object.defineProperty(object1, 'property1', {
value: 42,
writable: true,
enumerable: false,
configurable: true
});
is.emptyObject(object1);
2020-01-30 00:38:17 +07:00
//=> true
2018-09-28 11:54:35 +05:30
```
2018-09-28 13:30:19 +07:00
##### .nonEmptyObject(value)
2018-09-28 11:54:35 +05:30
Returns `true` if the value is an `Object` and `Object.keys(value).length` is more than 0.
##### .emptySet(value)
Returns `true` if the value is a `Set` and the `.size` is 0.
##### .nonEmptySet(Value)
Returns `true` if the value is a `Set` and the `.size` is more than 0.
##### .emptyMap(value)
Returns `true` if the value is a `Map` and the `.size` is 0.
##### .nonEmptyMap(value)
Returns `true` if the value is a `Map` and the `.size` is more than 0.
2017-09-22 00:44:27 +07:00
#### Miscellaneous
2017-12-09 11:55:08 -05:00
##### .directInstanceOf(value, class)
Returns `true` if `value` is a direct instance of `class` .
```js
is.directInstanceOf(new Error(), Error);
//=> true
2018-11-02 17:53:03 +07:00
class UnicornError extends Error {}
2017-12-09 11:55:08 -05:00
is.directInstanceOf(new UnicornError(), Error);
//=> false
```
2018-07-10 12:04:20 +03:00
##### .urlInstance(value)
Returns `true` if `value` is an instance of the [`URL` class ](https://developer.mozilla.org/en-US/docs/Web/API/URL ).
```js
const url = new URL('https://example.com');
is.urlInstance(url);
//=> true
```
2019-01-11 16:34:29 +07:00
##### .urlString(value)
2018-12-13 17:52:21 +02:00
Returns `true` if `value` is a URL string.
Note: this only does basic checking using the [`URL` class ](https://developer.mozilla.org/en-US/docs/Web/API/URL ) constructor.
```js
const url = 'https://example.com';
2018-12-13 23:38:35 +02:00
is.urlString(url);
2018-12-13 17:52:21 +02:00
//=> true
2018-12-13 23:38:35 +02:00
is.urlString(new URL(url));
2018-12-13 17:52:21 +02:00
//=> false
```
2017-10-20 14:49:52 +00:00
##### .truthy(value)
Returns `true` for all values that evaluate to true in a boolean context:
```js
is.truthy('🦄');
//=> true
is.truthy(undefined);
//=> false
```
##### .falsy(value)
2017-10-23 19:03:35 +07:00
Returns `true` if `value` is one of: `false` , `0` , `''` , `null` , `undefined` , `NaN` .
2017-10-20 14:49:52 +00:00
2017-09-22 00:44:27 +07:00
##### .nan(value)
##### .nullOrUndefined(value)
##### .primitive(value)
2023-08-07 08:50:03 +08:00
JavaScript primitives are as follows:
- `null`
- `undefined`
- `string`
- `number`
- `boolean`
- `symbol`
- `bigint`
2017-09-22 00:44:27 +07:00
##### .integer(value)
2017-10-17 11:19:17 -04:00
##### .safeInteger(value)
Returns `true` if `value` is a [safe integer ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger ).
2017-09-22 00:44:27 +07:00
##### .plainObject(value)
An object is plain if it's created by either `{}` , `new Object()` , or `Object.create(null)` .
##### .iterable(value)
2018-07-09 20:35:16 +03:00
##### .asyncIterable(value)
2017-09-24 22:26:15 +03:00
##### .class(value)
2023-08-10 22:05:22 +08:00
Returns `true` if the value is a class constructor.
2017-09-24 22:26:15 +03:00
2017-09-22 00:44:27 +07:00
##### .typedArray(value)
2017-10-26 09:30:17 -04:00
##### .arrayLike(value)
A `value` is array-like if it is not a function and has a `value.length` that is a safe integer greater than or equal to 0.
```js
is.arrayLike(document.forms);
//=> true
2018-11-02 17:53:03 +07:00
function foo() {
is.arrayLike(arguments);
//=> true
2017-10-26 09:30:17 -04:00
}
2018-11-02 17:53:03 +07:00
foo();
2017-10-26 09:30:17 -04:00
```
2023-07-23 15:35:23 +03:00
##### .tupleLike(value, guards)
A `value` is tuple-like if it matches the provided `guards` array both in `.length` and in types.
```js
is.tupleLike([1], [is.number]);
//=> true
```
```js
function foo() {
const tuple = [1, '2', true];
if (is.tupleLike(tuple, [is.number, is.string, is.boolean])) {
tuple // [number, string, boolean]
}
}
foo();
```
2024-01-05 23:30:28 -08:00
##### .positiveNumber(value)
2023-07-14 23:26:30 +01:00
Check if `value` is a number and is more than 0.
2024-01-05 23:30:28 -08:00
##### .negativeNumber(value)
2023-07-14 23:26:30 +01:00
Check if `value` is a number and is less than 0.
2017-09-25 20:20:06 +01:00
##### .inRange(value, range)
2017-09-26 02:32:58 +07:00
Check if `value` (number) is in the given `range` . The range is an array of two values, lower bound and upper bound, in no specific order.
```js
is.inRange(3, [0, 5]);
is.inRange(3, [5, 0]);
is.inRange(0, [-2, 2]);
```
##### .inRange(value, upperBound)
Check if `value` (number) is in the range of `0` to `upperBound` .
```js
is.inRange(3, 10);
```
2017-09-25 20:20:06 +01:00
2023-08-10 22:06:46 +08:00
##### .htmlElement(value)
2017-10-07 09:19:11 -07:00
2023-08-07 08:50:03 +08:00
Returns `true` if `value` is an [HTMLElement ](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement ).
2017-10-07 09:19:11 -07:00
2017-11-19 15:16:06 -05:00
##### .nodeStream(value)
Returns `true` if `value` is a Node.js [stream ](https://nodejs.org/api/stream.html ).
```js
2022-06-11 17:44:01 +07:00
import fs from 'node:fs';
2018-11-02 17:53:03 +07:00
2017-11-19 15:16:06 -05:00
is.nodeStream(fs.createReadStream('unicorn.png'));
//=> true
```
2018-05-03 05:22:32 +02:00
##### .observable(value)
Returns `true` if `value` is an `Observable` .
```js
2022-06-11 17:44:01 +07:00
import {Observable} from 'rxjs';
2018-11-02 17:53:03 +07:00
2018-05-03 05:22:32 +02:00
is.observable(new Observable());
//=> true
```
2017-10-05 00:18:25 -07:00
##### .infinite(value)
Check if `value` is `Infinity` or `-Infinity` .
2019-02-02 00:22:23 -06:00
##### .evenInteger(value)
2017-10-17 11:36:10 -04:00
Returns `true` if `value` is an even integer.
2019-02-02 00:22:23 -06:00
##### .oddInteger(value)
2017-10-17 11:36:10 -04:00
Returns `true` if `value` is an odd integer.
2021-09-10 10:26:13 +02:00
##### .propertyKey(value)
Returns `true` if `value` can be used as an object property key (either `string` , `number` , or `symbol` ).
2021-09-13 22:25:44 +08:00
##### .formData(value)
Returns `true` if `value` is an instance of the [`FormData` class ](https://developer.mozilla.org/en-US/docs/Web/API/FormData ).
```js
const data = new FormData();
is.formData(data);
//=> true
```
##### .urlSearchParams(value)
Returns `true` if `value` is an instance of the [`URLSearchParams` class ](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams ).
```js
const searchParams = new URLSearchParams();
is.urlSearchParams(searchParams);
//=> true
```
2020-02-16 19:06:19 -06:00
##### .any(predicate | predicate[], ...values)
2017-10-11 04:26:45 -05:00
2020-02-16 19:06:19 -06:00
Using a single `predicate` argument, returns `true` if **any** of the input `values` returns true in the `predicate` :
2017-10-11 04:26:45 -05:00
```js
is.any(is.string, {}, true, '🦄');
//=> true
is.any(is.boolean, 'unicorns', [], new Map());
//=> false
```
2020-02-16 19:06:19 -06:00
Using an array of `predicate[]` , returns `true` if **any** of the input `values` returns true for **any** of the `predicates` provided in an array:
```js
is.any([is.string, is.number], {}, true, '🦄');
//=> true
is.any([is.boolean, is.number], 'unicorns', [], new Map());
//=> false
```
2017-10-11 04:26:45 -05:00
##### .all(predicate, ...values)
Returns `true` if **all** of the input `values` returns true in the `predicate` :
```js
is.all(is.object, {}, new Map(), new Set());
//=> true
is.all(is.string, '🦄', [], 'unicorns');
//=> false
```
2024-02-29 08:23:30 +01:00
##### .validDate(value)
Returns `true` if the value is a valid date.
All [`Date` ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date ) objects have an internal timestamp value which is the number of milliseconds since the [Unix epoch ](https://developer.mozilla.org/en-US/docs/Glossary/Unix_time ). When a new `Date` is constructed with bad inputs, no error is thrown. Instead, a new `Date` object is returned. But the internal timestamp value is set to `NaN` , which is an `'Invalid Date'` . Bad inputs can be an non-parsable date string, a non-numeric value or a number that is outside of the expected range for a date value.
```js
const valid = new Date('2000-01-01');
is.date(valid);
//=> true
valid.getTime();
//=> 946684800000
valid.toUTCString();
//=> 'Sat, 01 Jan 2000 00:00:00 GMT'
is.validDate(valid);
//=> true
const invalid = new Date('Not a parsable date string');
is.date(invalid);
//=> true
invalid.getTime();
//=> NaN
invalid.toUTCString();
//=> 'Invalid Date'
is.validDate(invalid);
//=> false
```
2023-08-07 08:50:03 +08:00
##### .validLength(value)
Returns `true` if the value is a safe integer that is greater than or equal to zero.
This can be useful to confirm that a value is a valid count of something, ie. 0 or more.
##### .whitespaceString(value)
Returns `true` if the value is a string with only whitespace characters.
2019-02-12 14:40:44 +07:00
## Type guards
When using `is` together with TypeScript, [type guards ](http://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types ) are being used extensively to infer the correct type inside if-else statements.
```ts
import is from '@sindresorhus/is ';
const padLeft = (value: string, padding: string | number) => {
if (is.number(padding)) {
// `padding` is typed as `number`
return Array(padding + 1).join(' ') + value;
}
if (is.string(padding)) {
// `padding` is typed as `string`
return padding + value;
}
throw new TypeError(`Expected 'padding' to be of type 'string' or 'number', got '${is(padding)}'.` );
}
padLeft('🦄', 3);
//=> ' 🦄'
padLeft('🦄', '🌈');
//=> '🌈🦄'
```
2020-01-22 12:08:35 +01:00
## Type assertions
2020-01-22 18:47:01 +07:00
The type guards are also available as [type assertions ](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions ), which throw an error for unexpected types. It is a convenient one-line version of the often repetitive "if-not-expected-type-throw" pattern.
2020-01-22 12:08:35 +01:00
```ts
import {assert} from '@sindresorhus/is ';
const handleMovieRatingApiResponse = (response: unknown) => {
assert.plainObject(response);
2020-01-22 18:47:01 +07:00
// `response` is now typed as a plain `object` with `unknown` properties.
2020-01-22 12:08:35 +01:00
assert.number(response.rating);
2020-01-22 18:47:01 +07:00
// `response.rating` is now typed as a `number` .
2020-01-22 12:08:35 +01:00
assert.string(response.title);
2020-01-22 18:47:01 +07:00
// `response.title` is now typed as a `string` .
2020-01-22 12:08:35 +01:00
return `${response.title} (${response.rating * 10})` ;
};
handleMovieRatingApiResponse({rating: 0.87, title: 'The Matrix'});
//=> 'The Matrix (8.7)'
2020-01-22 18:47:01 +07:00
// This throws an error.
handleMovieRatingApiResponse({rating: '🦄'});
2020-01-22 12:08:35 +01:00
```
2019-02-12 14:40:44 +07:00
2020-01-29 17:22:35 +01:00
## Generic type parameters
2020-01-30 00:38:17 +07:00
The type guards and type assertions are aware of [generic type parameters ](https://www.typescriptlang.org/docs/handbook/generics.html ), such as `Promise<T>` and `Map<Key, Value>` . The default is `unknown` for most cases, since `is` cannot check them at runtime. If the generic type is known at compile-time, either implicitly (inferred) or explicitly (provided), `is` propagates the type so it can be used later.
2020-01-29 17:22:35 +01:00
Use generic type parameters with caution. They are only checked by the TypeScript compiler, and not checked by `is` at runtime. This can lead to unexpected behavior, where the generic type is _assumed_ at compile-time, but actually is something completely different at runtime. It is best to use `unknown` (default) and type-check the value of the generic type parameter at runtime with `is` or `assert` .
```ts
2020-01-30 00:38:17 +07:00
import {assert} from '@sindresorhus/is ';
2020-01-29 17:22:35 +01:00
async function badNumberAssumption(input: unknown) {
2020-01-30 00:51:52 +07:00
// Bad assumption about the generic type parameter fools the compile-time type system.
assert.promise< number > (input);
// `input` is a `Promise` but only assumed to be `Promise<number>` .
2020-01-29 17:22:35 +01:00
2020-01-30 00:51:52 +07:00
const resolved = await input;
// `resolved` is typed as `number` but was not actually checked at runtime.
2020-01-29 17:22:35 +01:00
2020-01-30 00:51:52 +07:00
// Multiplication will return NaN if the input promise did not actually contain a number.
return 2 * resolved;
2020-01-29 17:22:35 +01:00
}
async function goodNumberAssertion(input: unknown) {
2020-01-30 00:51:52 +07:00
assert.promise(input);
// `input` is typed as `Promise<unknown>`
2020-01-29 17:22:35 +01:00
2020-01-30 00:51:52 +07:00
const resolved = await input;
// `resolved` is typed as `unknown`
2020-01-29 17:22:35 +01:00
2020-01-30 00:51:52 +07:00
assert.number(resolved);
// `resolved` is typed as `number`
2020-01-29 17:22:35 +01:00
2020-01-30 00:51:52 +07:00
// Uses runtime checks so only numbers will reach the multiplication.
return 2 * resolved;
2020-01-29 17:22:35 +01:00
}
2020-01-30 00:38:17 +07:00
badNumberAssumption(Promise.resolve('An unexpected string'));
//=> NaN
2020-01-29 17:22:35 +01:00
// This correctly throws an error because of the unexpected string value.
2020-01-30 00:38:17 +07:00
goodNumberAssertion(Promise.resolve('An unexpected string'));
2020-01-29 17:22:35 +01:00
```
2017-09-22 00:44:27 +07:00
## FAQ
### Why yet another type checking module?
There are hundreds of type checking modules on npm, unfortunately, I couldn't find any that fit my needs:
- Includes both type methods and ability to get the type
- Types of primitives returned as lowercase and object types as camelcase
- Covers all built-ins
- Unsurprising behavior
- Well-maintained
- Comprehensive test suite
For the ones I found, pick 3 of these.
The most common mistakes I noticed in these modules was using `instanceof` for type checking, forgetting that functions are objects, and omitting `symbol` as a primitive.
2021-10-31 12:34:45 +07:00
### Why not just use `instanceof` instead of this package?
`instanceof` does not work correctly for all types and it does not work across [realms ](https://stackoverflow.com/a/49832343/64949 ). Examples of realms are iframes, windows, web workers, and the `vm` module in Node.js.
2017-09-22 00:44:27 +07:00
## Related
2024-04-28 23:54:22 +07:00
- [environment ](https://github.com/sindresorhus/environment ) - Check which JavaScript environment your code is running in at runtime
2017-09-22 00:44:27 +07:00
- [is-stream ](https://github.com/sindresorhus/is-stream ) - Check if something is a Node.js stream
- [is-observable ](https://github.com/sindresorhus/is-observable ) - Check if a value is an Observable
- [file-type ](https://github.com/sindresorhus/file-type ) - Detect the file type of a Buffer/Uint8Array
- [is-ip ](https://github.com/sindresorhus/is-ip ) - Check if a string is an IP address
- [is-array-sorted ](https://github.com/sindresorhus/is-array-sorted ) - Check if an Array is sorted
- [is-error-constructor ](https://github.com/sindresorhus/is-error-constructor ) - Check if a value is an error constructor
- [is-empty-iterable ](https://github.com/sindresorhus/is-empty-iterable ) - Check if an Iterable is empty
2017-10-06 00:48:04 -07:00
- [is-blob ](https://github.com/sindresorhus/is-blob ) - Check if a value is a Blob - File-like object of immutable, raw data
2017-11-08 17:22:23 +07:00
- [has-emoji ](https://github.com/sindresorhus/has-emoji ) - Check whether a string has any emoji
2017-09-22 00:44:27 +07:00
2019-06-30 15:15:27 +07:00
## Maintainers
2017-09-28 13:26:23 +07:00
- [Sindre Sorhus ](https://github.com/sindresorhus )
- [Giora Guttsait ](https://github.com/gioragutt )
2017-10-23 19:04:11 +07:00
- [Brandon Smith ](https://github.com/brandon93s )