Minor readme tweaks

This commit is contained in:
Sindre Sorhus 2020-01-30 00:51:52 +07:00
parent 863e26ad6a
commit f97029fd73

View file

@ -509,29 +509,29 @@ Use generic type parameters with caution. They are only checked by the TypeScrip
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.
assert.promise<number>(input); assert.promise<number>(input);
// `input` is a `Promise` but only assumed to be `Promise<number>`. // `input` is a `Promise` but only assumed to be `Promise<number>`.
const resolved = await input; const resolved = await input;
// `resolved` is typed as `number` but was not actually checked at runtime. // `resolved` is typed as `number` but was not actually checked at runtime.
// Multiplication will return NaN if the input promise did not actually contain a number. // Multiplication will return NaN if the input promise did not actually contain a number.
return 2 * resolved; return 2 * resolved;
} }
async function goodNumberAssertion(input: unknown) { async function goodNumberAssertion(input: unknown) {
assert.promise(input); assert.promise(input);
// `input` is typed as `Promise<unknown>` // `input` is typed as `Promise<unknown>`
const resolved = await input; const resolved = await input;
// `resolved` is typed as `unknown` // `resolved` is typed as `unknown`
assert.number(resolved); assert.number(resolved);
// `resolved` is typed as `number` // `resolved` is typed as `number`
// Uses runtime checks so only numbers will reach the multiplication. // Uses runtime checks so only numbers will reach the multiplication.
return 2 * resolved; return 2 * resolved;
} }
badNumberAssumption(Promise.resolve('An unexpected string')); badNumberAssumption(Promise.resolve('An unexpected string'));