Meta tweaks

This commit is contained in:
Sindre Sorhus 2020-01-30 00:38:17 +07:00
parent 446a7a081e
commit 5d0ccec21c
2 changed files with 18 additions and 18 deletions

View file

@ -51,12 +51,12 @@
"@types/jsdom": "^12.2.4", "@types/jsdom": "^12.2.4",
"@types/node": "^12.12.6", "@types/node": "^12.12.6",
"@types/zen-observable": "^0.8.0", "@types/zen-observable": "^0.8.0",
"@typescript-eslint/eslint-plugin": "^2.17.0", "@typescript-eslint/eslint-plugin": "^2.18.0",
"@typescript-eslint/parser": "^2.17.0", "@typescript-eslint/parser": "^2.18.0",
"ava": "^2.1.0", "ava": "^2.1.0",
"del-cli": "^2.0.0", "del-cli": "^2.0.0",
"eslint-config-xo-typescript": "^0.24.1", "eslint-config-xo-typescript": "^0.24.1",
"jsdom": "^15.0.0", "jsdom": "^16.0.1",
"rxjs": "^6.4.0", "rxjs": "^6.4.0",
"tempy": "^0.3.0", "tempy": "^0.3.0",
"ts-node": "^8.3.0", "ts-node": "^8.3.0",

View file

@ -126,10 +126,10 @@ Returns `true` for any `async` function that can be called with the `await` oper
```js ```js
is.asyncFunction(async () => {}); is.asyncFunction(async () => {});
// => true //=> true
is.asyncFunction(() => {}); is.asyncFunction(() => {});
// => false //=> false
``` ```
##### .asyncGenerator(value) ##### .asyncGenerator(value)
@ -140,14 +140,14 @@ is.asyncGenerator(
yield 4; yield 4;
})() })()
); );
// => true //=> true
is.asyncGenerator( is.asyncGenerator(
(function * () { (function * () {
yield 4; yield 4;
})() })()
); );
// => false //=> false
``` ```
##### .asyncGeneratorFunction(value) ##### .asyncGeneratorFunction(value)
@ -156,12 +156,12 @@ is.asyncGenerator(
is.asyncGeneratorFunction(async function * () { is.asyncGeneratorFunction(async function * () {
yield 4; yield 4;
}); });
// => true //=> true
is.asyncGeneratorFunction(function * () { is.asyncGeneratorFunction(function * () {
yield 4; yield 4;
}); });
// => false //=> false
``` ```
##### .boundFunction(value) ##### .boundFunction(value)
@ -170,13 +170,13 @@ Returns `true` for any `bound` function.
```js ```js
is.boundFunction(() => {}); is.boundFunction(() => {});
// => true //=> true
is.boundFunction(function () {}.bind(null)); is.boundFunction(function () {}.bind(null));
// => true //=> true
is.boundFunction(function () {}); is.boundFunction(function () {});
// => false //=> false
``` ```
##### .map(value) ##### .map(value)
@ -243,7 +243,7 @@ Object.defineProperty(object1, 'property1', {
}); });
is.emptyObject(object1); is.emptyObject(object1);
// => true //=> true
``` ```
##### .nonEmptyObject(value) ##### .nonEmptyObject(value)
@ -501,12 +501,12 @@ handleMovieRatingApiResponse({rating: '🦄'});
## Generic type parameters ## 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`. 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 ```ts
import { assert } from '@sindresorhus/is'; import {assert} from '@sindresorhus/is';
async function badNumberAssumption(input: unknown) { async function badNumberAssumption(input: unknown) {
// Bad assumption about the generic type parameter fools the compile-time type system. // Bad assumption about the generic type parameter fools the compile-time type system.
@ -534,11 +534,11 @@ async function goodNumberAssertion(input: unknown) {
return 2 * resolved; return 2 * resolved;
} }
badNumberAssumption(Promise.resolve("an unexpected string")); badNumberAssumption(Promise.resolve('An unexpected string'));
//=> 'NaN' //=> NaN
// This correctly throws an error because of the unexpected string value. // This correctly throws an error because of the unexpected string value.
goodNumberAssertion(Promise.resolve("an unexpected string")); goodNumberAssertion(Promise.resolve('An unexpected string'));
``` ```
## FAQ ## FAQ