digamos que tengo esto
export type Hash = [ hashtype, hash ]; export type hashtype = -16 | -43 | 5 | 6; export type hash = Buffer;Quiero escribir algo que verifique si un objeto es un Hash
no se ha implementado
isHash = (obj: any) => { return (obj is Hash) // pseudo code, to implement }Para que tuviera tal retorno:
isHash(5) => false // no hash isHash([25, <Buffer ad 30>]) => false // 25 is not in hashType isHash([5, <Buffer ad 30>]) => true // validNo hay una forma de propósito general para verificar si un tipo coincide. Para su caso específico, haría algo como esto:
const isHash = (obj: unknown): obj is Hash => { if (!Array.isArray(obj)) { return false; } if (obj.length !== 2) { return false; } return [-16, -43, 5, 6].includes(obj[0]) && Buffer.isBuffer(obj[1]); }