From 5d54aa7dddaacf73684f9f350b8d21255dec46ff Mon Sep 17 00:00:00 2001 From: Sam Verschueren Date: Tue, 3 Apr 2018 07:50:52 +0200 Subject: [PATCH] Document type guards --- readme.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/readme.md b/readme.md index 67fad06..ebcb9d8 100644 --- a/readme.md +++ b/readme.md @@ -27,6 +27,32 @@ 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