Skip to content

Commit

Permalink
feat(ui): Add TS definitions for "is" utils (#14456)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufkandemir committed Sep 21, 2022
1 parent 27a07f6 commit 21030f5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions ui/types/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export * from './utils/dom';
export * from './utils/event';
export * from './utils/format';
export * from './utils/scroll';
export * from './utils/is';
export * from './utils/run-sequential-promises';

import { VueStyleObjectProp } from "./api/vue-prop-types";
Expand Down
67 changes: 67 additions & 0 deletions ui/types/utils/is.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
interface is {
/**
* Recursively checks if one Object is equal to another.
* Also supports Map, Set, ArrayBuffer, Regexp, Date, and many more.
*
* @example
* const objA = { foo: 1, bar: { baz: 2 } }
* const objB = { foo: 1, bar: { baz: 2 } }
* objA === objB // false
* is.deepEqual(objA, objB) // true
*/
// Can't do it both ways, see: https://github.com/microsoft/TypeScript/issues/26916#issuecomment-555691585
deepEqual<T>(obj1: T, obj2: unknown): obj2 is T;

/**
* Checks if a value is an object.
* Note: Map, Set, ArrayBuffer, Regexp, Date, etc. are also objects,
* but null and Arrays are not.
*
* @example
* is.object({}) // true
* is.object([]) // false
* is.object(null) // false
* is.object(1) // false
* is.object(new Map()) // true
* is.object(new Set()) // true
*/
object<T>(val: T): val is Record<any, any>;

/**
* Checks if a value is a Date object.
*
* @see `date.isValid()` If you want to check if a value is a valid date string or number
*
* @example
* is.date(new Date()) // true
* is.date(Date.now()) // false
* is.date(new Date('2022-09-24T11:00:00.000Z')) // true
* is.date('2022-09-24') // false
*/
date(val: unknown): val is Date;

/**
* Checks if a value is a RegExp.
*
* @example <caption>Literal notation</caption>
* is.regexp(/foo/); // true
* @example <caption>Constructor</caption>
* is.regexp(new RegExp('bar', 'g')); // true
*/
regexp(val: unknown): val is RegExp;

/**
* Checks if a value is a finite number.
* Note: BigInts, NaN, and Infinity values are not considered as numbers.
*
* @example
* is.number(1); // true
* is.number('1'); // false
* is.number(1n); // false
* is.number(NaN); // false
* is.number(Infinity); // false
*/
number(val: unknown): val is number;
}

export declare const is: is;

0 comments on commit 21030f5

Please sign in to comment.