Add documentation for named exports and name default method as detect

This commit is contained in:
= 2023-08-03 09:21:14 +09:00
parent 66943d06b3
commit 76303b8a3a
2 changed files with 82 additions and 63 deletions

View file

@ -72,11 +72,17 @@ Example:
- `'Function'`
- `'Object'`
This method is also exported as `detect`, you can import it like this:
```js
import {detect} from '@sindresorhus/is';
```
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')`.
### is.{method}
All the below methods accept a value and returns a boolean for whether the value is of the desired type.
All the below methods accept a value and return a boolean for whether the value is of the desired type.
#### Primitives
@ -566,6 +572,18 @@ This can be useful to confirm that a value is a valid count of something, ie. 0
Returns `true` if the value is a string with only whitespace characters.
## 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 above 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';
```
## 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.