Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 29, 2020
1 parent 446a7a0 commit 5d0ccec
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
"@types/jsdom": "^12.2.4",
"@types/node": "^12.12.6",
"@types/zen-observable": "^0.8.0",
"@typescript-eslint/eslint-plugin": "^2.17.0",
"@typescript-eslint/parser": "^2.17.0",
"@typescript-eslint/eslint-plugin": "^2.18.0",
"@typescript-eslint/parser": "^2.18.0",
"ava": "^2.1.0",
"del-cli": "^2.0.0",
"eslint-config-xo-typescript": "^0.24.1",
"jsdom": "^15.0.0",
"jsdom": "^16.0.1",
"rxjs": "^6.4.0",
"tempy": "^0.3.0",
"ts-node": "^8.3.0",
Expand Down
30 changes: 15 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ Returns `true` for any `async` function that can be called with the `await` oper

```js
is.asyncFunction(async () => {});
// => true
//=> true

is.asyncFunction(() => {});
// => false
//=> false
```

##### .asyncGenerator(value)
Expand All @@ -140,14 +140,14 @@ is.asyncGenerator(
yield 4;
})()
);
// => true
//=> true

is.asyncGenerator(
(function * () {
yield 4;
})()
);
// => false
//=> false
```

##### .asyncGeneratorFunction(value)
Expand All @@ -156,12 +156,12 @@ is.asyncGenerator(
is.asyncGeneratorFunction(async function * () {
yield 4;
});
// => true
//=> true

is.asyncGeneratorFunction(function * () {
yield 4;
});
// => false
//=> false
```

##### .boundFunction(value)
Expand All @@ -170,13 +170,13 @@ Returns `true` for any `bound` function.

```js
is.boundFunction(() => {});
// => true
//=> true

is.boundFunction(function () {}.bind(null));
// => true
//=> true

is.boundFunction(function () {});
// => false
//=> false
```

##### .map(value)
Expand Down Expand Up @@ -243,7 +243,7 @@ Object.defineProperty(object1, 'property1', {
});

is.emptyObject(object1);
// => true
//=> true
```

##### .nonEmptyObject(value)
Expand Down Expand Up @@ -501,12 +501,12 @@ handleMovieRatingApiResponse({rating: '🦄'});

## Generic type parameters

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` can not 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.
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.

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
import { assert } from '@sindresorhus/is';
import {assert} from '@sindresorhus/is';

async function badNumberAssumption(input: unknown) {
// Bad assumption about the generic type parameter fools the compile-time type system.
Expand Down Expand Up @@ -534,11 +534,11 @@ async function goodNumberAssertion(input: unknown) {
return 2 * resolved;
}

badNumberAssumption(Promise.resolve("an unexpected string"));
//=> 'NaN'
badNumberAssumption(Promise.resolve('An unexpected string'));
//=> NaN

// This correctly throws an error because of the unexpected string value.
goodNumberAssertion(Promise.resolve("an unexpected string"));
goodNumberAssertion(Promise.resolve('An unexpected string'));
```

## FAQ
Expand Down

0 comments on commit 5d0ccec

Please sign in to comment.