Saltar al contenido

CódigoSin intentar

Funciones auxiliares de comprobación de tipos

Dificultad: fácil

Implementa isArray, isFunction, isObject e isPlainObject.

  • isObject: los objetos y las funciones son objetos; null no (recuerda typeof null)
  • isPlainObject: solo {}, new Object() y Object.create(null), no arrays, fechas ni instancias de clase
Ver solución de referencia
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;
}
Cargando editor…