diff --git a/package.json b/package.json index 619844e..cd35b89 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@sindresorhus/is", "version": "0.15.0", - "description": "Type check values: `is.string('🦄') //=> true`", + "description": "Type check values", "license": "MIT", "repository": "sindresorhus/is", "author": { @@ -41,7 +41,10 @@ "kind", "primitive", "verify", - "compare" + "compare", + "typescript", + "typeguards", + "types" ], "devDependencies": { "@sindresorhus/tsconfig": "^0.2.0", diff --git a/readme.md b/readme.md index ed66af6..848a172 100644 --- a/readme.md +++ b/readme.md @@ -1,10 +1,20 @@ # is [![Build Status](https://travis-ci.org/sindresorhus/is.svg?branch=master)](https://travis-ci.org/sindresorhus/is) -> Type check values: `is.string('🦄') //=> true` +> Type check values + +For example, `is.string('🦄') //=> true` +## Highlights + +- Written in TypeScript +- [Extensive use of type guards](#type-guards) +- Actively maintained +- 2 million weekly downloads + + ## Install ``` @@ -27,32 +37,6 @@ is.number(6); //=> true ``` -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 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('🦄', '🌈'); -//=> '🌈🦄' -``` - ## API @@ -407,6 +391,35 @@ is.all(is.string, '🦄', [], 'unicorns'); ``` +## 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('🦄', '🌈'); +//=> '🌈🦄' +``` + + ## FAQ ### Why yet another type checking module?