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.

1

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

-1

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
}

1 Comment

[ERROR]: Property 'code' does not exist on type 'CustomError'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.