1

I have this function which used to work in plain javascript. I am now trying to "convert" it to Typescript.

function checkStatus(response :any) {
  if (response.status >= 200 && response.status < 300) {
    return response;
  } else {
    const error = new Error(`HTTP Error ${response.statusText}`);
    error.status = response.statusText;
    error.response = response;
    console.log(error);
    throw error;
  }
}

The red squiggly lines in my editor show that:

[ts] Property 'status' does not exist on type 'Error'. any

and

[ts] Property 'response' does not exist on type 'Error'. any

So is there a typescript version of the Error object defined somewhere in the Typescript land and its missing status and response fields on the Error object? Thank you

1
  • You can extends Error with your own version with new properties. Default one doesn't have status and response. Commented Jan 29, 2018 at 22:59

2 Answers 2

2

The Error object does not by default have those properties. You can do this in Javascript because you can add any property to any object in Javascript. You can do one of two things:

Add the properties on the Error interface

Just declare an interface with the same name, in the global namespace. In typescript multiple interface declarations are merged and the resulting interface is used:

interface Error {
    status: string;
    response: HttpRequest
}

Create a dedicated HttpError

This is probably a better approach, as the properties will not be present on any error. Just define a class derived from error that has the extra properties:

class HttpError extends Error {
    status: string;
    response: HttpRequest
}

const error = new HttpError(`HTTP Error ${response.statusText}`);
error.status = response.statusText;
error.response = response;
Sign up to request clarification or add additional context in comments.

Comments

1

So is there a typescript version of the Error object defined somewhere in the Typescript land and its missing status and response fields on the Error object?

Yes, Error object in typescript is defined in the library type definition file and it does not have status and response properties:

interface Error {
    name: string;
    message: string;
    stack?: string;
}

One possible way to assign properties to the object that are not defined in the declared type of that object is by using Object.assign():

function checkStatus(response :any) {
    if (response.status >= 200 && response.status < 300) {
        return response;
    } else {
        const error = Object.assign(
            new Error(`HTTP Error ${response.statusText}`),
            {
                status: response.statusText,
                response
            }
        );
        console.log(error);
        throw error;
  }
}

Comments

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.