Add type guards (#43)

This commit is contained in:
Sam Verschueren 2018-04-03 19:05:59 +02:00 committed by Sindre Sorhus
parent 5670ed7a97
commit 8894dbec17
3 changed files with 82 additions and 44 deletions

View file

@ -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

View file

@ -1,5 +1,12 @@
import * as util from 'util';
type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
type Primitive = null | undefined | string | number | boolean | Symbol;
export interface ArrayLike {
length: number;
}
export const enum TypeName {
null = 'null',
boolean = 'boolean',
@ -36,7 +43,7 @@ export const enum TypeName {
}
const toString = Object.prototype.toString;
const isOfType = (type: string) => (value: any) => typeof value === type; // tslint:disable-line:strict-type-predicates
const isOfType = <T>(type: string) => (value: any): value is T => typeof value === type; // tslint:disable-line:strict-type-predicates
const getObjectType = (value: any): TypeName | null => {
const objectName = toString.call(value).slice(8, -1) as string;
@ -48,7 +55,7 @@ const getObjectType = (value: any): TypeName | null => {
return null;
};
const isObjectOfType = (type: TypeName) => (value: any) => getObjectType(value) === type;
const isObjectOfType = <T>(type: TypeName) => (value: any): value is T => getObjectType(value) === type;
function is(value: any): TypeName { // tslint:disable-line:only-arrow-functions
if (value === null) {
@ -105,60 +112,60 @@ namespace is { // tslint:disable-line:no-namespace
const isObject = (value: any) => typeof value === 'object';
// tslint:disable:variable-name
export const undefined = isOfType('undefined');
export const string = isOfType('string');
export const number = isOfType('number');
export const function_ = isOfType('function');
export const null_ = (value: any) => value === null;
export const undefined = isOfType<undefined>('undefined');
export const string = isOfType<string>('string');
export const number = isOfType<number>('number');
export const function_ = isOfType<Function>('function');
export const null_ = (value: any): value is null => value === null;
export const class_ = (value: any) => function_(value) && value.toString().startsWith('class ');
export const boolean = (value: any) => value === true || value === false;
export const symbol = isOfType('symbol');
export const boolean = (value: any): value is boolean => value === true || value === false;
export const symbol = isOfType<Symbol>('symbol');
// tslint:enable:variable-name
export const array = Array.isArray;
export const buffer = Buffer.isBuffer;
export const nullOrUndefined = (value: any) => null_(value) || undefined(value);
export const nullOrUndefined = (value: any): value is null | undefined => null_(value) || undefined(value);
export const object = (value: any) => !nullOrUndefined(value) && (function_(value) || isObject(value));
export const iterable = (value: any) => !nullOrUndefined(value) && function_(value[Symbol.iterator]);
export const generator = (value: any) => iterable(value) && function_(value.next) && function_(value.throw);
export const iterable = (value: any): value is Iterator<any> => !nullOrUndefined(value) && function_(value[Symbol.iterator]);
export const generator = (value: any): value is Generator => iterable(value) && function_(value.next) && function_(value.throw);
export const nativePromise = isObjectOfType(TypeName.Promise);
export const nativePromise = isObjectOfType<Promise<any>>(TypeName.Promise);
const hasPromiseAPI = (value: any) =>
const hasPromiseAPI = (value: any): value is Promise<any> =>
!null_(value) &&
isObject(value) &&
function_(value.then) &&
function_(value.catch);
export const promise = (value: any) => nativePromise(value) || hasPromiseAPI(value);
export const promise = (value: any): value is Promise<any> => nativePromise(value) || hasPromiseAPI(value);
const isFunctionOfType = (type: TypeName) => isObjectOfType(type);
export const generatorFunction = isFunctionOfType(TypeName.GeneratorFunction);
export const asyncFunction = isFunctionOfType(TypeName.AsyncFunction);
export const boundFunction = (value: any) => function_(value) && !value.hasOwnProperty('prototype');
const isFunctionOfType = <T>(type: TypeName) => isObjectOfType<T>(type);
export const generatorFunction = isFunctionOfType<GeneratorFunction>(TypeName.GeneratorFunction);
export const asyncFunction = isFunctionOfType<Function>(TypeName.AsyncFunction);
export const boundFunction = (value: any): value is Function => function_(value) && !value.hasOwnProperty('prototype');
export const regExp = isObjectOfType(TypeName.RegExp);
export const date = isObjectOfType(TypeName.Date);
export const error = isObjectOfType(TypeName.Error);
export const map = isObjectOfType(TypeName.Map);
export const set = isObjectOfType(TypeName.Set);
export const weakMap = isObjectOfType(TypeName.WeakMap);
export const weakSet = isObjectOfType(TypeName.WeakSet);
export const regExp = isObjectOfType<RegExp>(TypeName.RegExp);
export const date = isObjectOfType<Date>(TypeName.Date);
export const error = isObjectOfType<Error>(TypeName.Error);
export const map = isObjectOfType<Map<any, any>>(TypeName.Map);
export const set = isObjectOfType<Set<any>>(TypeName.Set);
export const weakMap = isObjectOfType<WeakMap<any, any>>(TypeName.WeakMap);
export const weakSet = isObjectOfType<WeakSet<any>>(TypeName.WeakSet);
export const int8Array = isObjectOfType(TypeName.Int8Array);
export const uint8Array = isObjectOfType(TypeName.Uint8Array);
export const uint8ClampedArray = isObjectOfType(TypeName.Uint8ClampedArray);
export const int16Array = isObjectOfType(TypeName.Int16Array);
export const uint16Array = isObjectOfType(TypeName.Uint16Array);
export const int32Array = isObjectOfType(TypeName.Int32Array);
export const uint32Array = isObjectOfType(TypeName.Uint32Array);
export const float32Array = isObjectOfType(TypeName.Float32Array);
export const float64Array = isObjectOfType(TypeName.Float64Array);
export const int8Array = isObjectOfType<Int8Array>(TypeName.Int8Array);
export const uint8Array = isObjectOfType<Uint8Array>(TypeName.Uint8Array);
export const uint8ClampedArray = isObjectOfType<Uint8ClampedArray>(TypeName.Uint8ClampedArray);
export const int16Array = isObjectOfType<Int16Array>(TypeName.Int16Array);
export const uint16Array = isObjectOfType<Uint16Array>(TypeName.Uint16Array);
export const int32Array = isObjectOfType<Int32Array>(TypeName.Int32Array);
export const uint32Array = isObjectOfType<Uint32Array>(TypeName.Uint32Array);
export const float32Array = isObjectOfType<Float32Array>(TypeName.Float32Array);
export const float64Array = isObjectOfType<Float64Array>(TypeName.Float64Array);
export const arrayBuffer = isObjectOfType(TypeName.ArrayBuffer);
export const sharedArrayBuffer = isObjectOfType(TypeName.SharedArrayBuffer);
export const dataView = isObjectOfType(TypeName.DataView);
export const arrayBuffer = isObjectOfType<ArrayBuffer>(TypeName.ArrayBuffer);
export const sharedArrayBuffer = isObjectOfType<SharedArrayBuffer>(TypeName.SharedArrayBuffer);
export const dataView = isObjectOfType<DataView>(TypeName.DataView);
export const directInstanceOf = (instance: any, klass: any) => Object.getPrototypeOf(instance) === klass.prototype;
@ -175,10 +182,10 @@ namespace is { // tslint:disable-line:no-namespace
'symbol'
]);
export const primitive = (value: any) => null_(value) || primitiveTypes.has(typeof value);
export const primitive = (value: any): value is Primitive => null_(value) || primitiveTypes.has(typeof value);
export const integer = (value: any) => Number.isInteger(value);
export const safeInteger = (value: any) => Number.isSafeInteger(value);
export const integer = (value: any): value is number => Number.isInteger(value);
export const safeInteger = (value: any): value is number => Number.isSafeInteger(value);
export const plainObject = (value: any) => {
// From: https://github.com/sindresorhus/is-plain-obj/blob/master/index.js
@ -200,7 +207,7 @@ namespace is { // tslint:disable-line:no-namespace
TypeName.Float32Array,
TypeName.Float64Array
]);
export const typedArray = (value: any) => {
export const typedArray = (value: any): value is TypedArray => {
const objectType = getObjectType(value);
if (objectType === null) {
@ -211,11 +218,11 @@ namespace is { // tslint:disable-line:no-namespace
};
const isValidLength = (value: any) => safeInteger(value) && value > -1;
export const arrayLike = (value: any) => !nullOrUndefined(value) && !function_(value) && isValidLength(value.length);
export const arrayLike = (value: any): value is ArrayLike => !nullOrUndefined(value) && !function_(value) && isValidLength(value.length);
export const inRange = (value: number, range: number | number[]) => {
if (number(range)) {
return value >= Math.min(0, range as number) && value <= Math.max(range as number, 0);
return value >= Math.min(0, range) && value <= Math.max(range, 0);
}
if (array(range) && range.length === 2) {

View file

@ -7,6 +7,11 @@
"declaration": true,
"pretty": true,
"newLine": "lf",
"lib": [
"dom",
"es2015",
"es2017.sharedmemory"
],
"stripInternal": true,
"noImplicitAny": true,
"noImplicitReturns": true,