Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
Check if object is of type (type is defined as array) - TypeScript

Let's say I have this

export type Hash = [ hashtype, hash ];

export type hashtype = -16 | -43 | 5 | 6;
export type hash = Buffer;

I want to write something that will check whether an object is a Hash

not implemented

isHash = (obj: any) => {
    return (obj is Hash) // pseudo code, to implement
}

So that I would have such a return:

isHash(5)                         => false    //  no hash
isHash([25, <Buffer ad 30>])      => false    //  25 is not in hashType

isHash([5, <Buffer ad 30>])       => true     //  valid
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There isn't a general-purpose way to check whether a type matches. For your specific case, I would do something like this:

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]);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!