Ir para o conteúdo

CódigoNão tentada

Helpers de verificação de tipo

Dificuldade: fácil

Implemente isArray, isFunction, isObject e isPlainObject.

  • isObject: objetos e funções são objetos; null não é (lembre do typeof null)
  • isPlainObject: apenas {}, new Object() e Object.create(null), não arrays, datas nem instâncias de classe
Ver solução de referência
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;
}
Carregando editor…