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

197
Views
error instanceof X doesn't work as expected

I have a class called UserInputError that extends Error

export default class UserInputError extends Error {
    constructor(message: string) {
        super(message);
        this.name = "UserInputError";
    }
}

When i try to catch this specific error i use

try {
     // some code
} catch(e: any) {
    // catch logic here
    console.log(e instanceof UserInputError, e.name === "UserInputError");

The output is

false true

why instanceof doesn't work as expected? does context matter? or was it because I've used any?

Edit:

This is the code snippet that throws the error

if (invalidNumberOfMethods) {
    throw new UserInputError("you must chose one method");
}

NOTE1: I don't throw UserInputError instead of throw new UserInputError

> grep -R --include "*.ts" --exclude-dir=node_modules "throw UserInputError" .
> echo $?
> 1

NOTE2: I'm using this with NodeJs, not the browser.

about 4 years ago · Juan Pablo Isaza
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!