Skip to content

CodeNot attempted

Type-check helpers

Difficulty: easy

Implement isArray, isFunction, isObject and isPlainObject.

  • isObject: objects and functions are objects; null is not (remember typeof null)
  • isPlainObject: only {}, new Object() and Object.create(null), not arrays, dates or class instances
Show reference solution
export function isArray(value: unknown): value is unknown[] {
return Array.isArray(value);
}
export function isFunction(value: unknown): value is (...args: unknown[]) => unknown {
return typeof value === 'function';
}
export function isObject(value: unknown): value is object {
if (value === null || value === undefined) {
return false; // typeof null is 'object'
}
return typeof value === 'object' || typeof value === 'function';
}
export function isPlainObject(value: unknown): value is Record<string, unknown> {
if (value === null || typeof value !== 'object') {
return false;
}
const prototype = Object.getPrototypeOf(value);
return prototype === null || prototype === Object.prototype;
}
Loading editor…