1

I having been doing Swift Development for past 3 years and recently switch to typescript.

In Swift an error of type NSError and which gives us some properties.

In JavaScript an error is of any.

Why error is of any? How can I create custom class with some properties and use it in whole project?

try {
    const decodedResult = decode(token)
    // Adding userId in the headers to use in the models.
    req.headers.userId = decodedResult.paylod.id
    next()
} catch (error) {
    console.log(error)
    new errorController(401, 'Authentication required.', res)
}

Now error object here is of type any. I want it to be strongly typed.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Anirudha Mahale
  • 2,526
  • 3
  • 37
  • 57
  • 1
    Possible duplicate of [Custom error class in TypeScript](https://stackoverflow.com/questions/31626231/custom-error-class-in-typescript) – Pankaj Prakash Jun 03 '19 at 09:59

3 Answers3

2

Any are the type of variables that we do not know when we are writing an application. These values may come from dynamic content, e.g. from the user or a 3rd party library. In these cases, we want to opt-out of type checking and let the values pass through compile-time checks. To do so, we label these with the any type.

let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean

You can extend Error Class in typescript to create your custom error handler

 class MyError extends Error {
        constructor(m: string) {
            super(m);
}

        anyFunction() {
            return "Hello World " ;
        }
    }


    try {
        const decodedResult = decode(token)
        // Adding userId in the headers to use in the models.
        req.headers.userId = decodedResult.paylod.id
        next()
    } catch (error) {
        console.log(error)
        throw new MyError()  // pass arguments of constructor if any
    }

See Reference

Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46
1

As mentioned in https://basarat.gitbooks.io/typescript/docs/types/exceptions.html you can throw something like new Error() (as well as any other thing because it will be transpilled into javascirpt at the end)

Hence you could create a new class such as

         export class MySpecificError extends Error

That you could throw in you methods and catch in your catchclauses However this will only work for code you write yourself

CharybdeBE
  • 1,791
  • 1
  • 20
  • 35
0

Thats because you can throw anything in Javascript:

  throw 1;

As any deeply nested code could throw something somewhere, it is impossible for Typescript to correctly type it, thats why "error" has an any type.

Typeguards however help you to narrow down a specific error type:

class CustomError extends Error {
    constructor(public code: number, message: string) {
        super(message);
    }
}

try {
    throw new CustomError(404, "Not Found");
} catch(error) {
    if(!(error instanceof CustomError))
    throw error;

    console.log(error.code); // correctly typed here
}
Steven Kanberg
  • 6,078
  • 2
  • 16
  • 35
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151